I am trying to get the first 4 rows of an xlsx file to repeat at the top of each page when printed. I am using the Open XML SDK to accomplish this.
My file is bei
This is possible via ClosedXML
Worksheet.PageSetup.SetRowsToRepeatAtTop(1, 5);
Update:
@Duncan - After your comment, I try to find out root cause of file corrupt. It turns to be SpreadsheetPinterSettings
is binary data in the document. Even adding the DefinedName _xlnm.Print_Titles
to Spreadsheet; it doesn't have any effect during a Print/Print Preview.
It seems Open XML approach for handling spreadsheet printer settings is not helpful.
Reflect code from Open XML SDK Productivity tool: manually created Print Titles in MS Excel
// Adds child parts and generates content of the specified part.
public void CreateSpreadsheetPrinterSettingsPart(SpreadsheetPrinterSettingsPart part)
{
GeneratePartContent(part);
}
// Generates content of part.
private void GeneratePartContent(SpreadsheetPrinterSettingsPart part)
{
System.IO.Stream data = GetBinaryDataStream(partData);
part.FeedData(data);
data.Close();
}
#region Binary Data
private string partData = "MQAyADcALgAwAC4AMAAuADEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEEAAbcAHweXf+AAwEAAH/qCm8IZAABAA8A/P8CAAEA/P8DAAEATABlAHQAdABlAHIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAABAAAAAgAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFBSSVbiMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAQJxAnECcAABAnAAAAAAAAAABYAywGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAABQGBAAXEsDAGhDBAAAAAAAAAAAAAEAAQAAAAAAAAAAAAAAAAAAAAAA8q/OjRwAAAAAAAAAAAABAAAAAAD/AP8AAAABAAAAAAAC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wAAAAABAAAPAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAIAAAAAAQwA6AFwAUAByAG8AZwByAGEAbQAgAEYAaQBsAGUAcwBcAE0AaQBjAHIAbwBzAG8AZgB0ACAATwBmAGYAaQBjAGUAXABPAGYAZgBpAGMAZQAxADQAXABFAFgAQwBFAEwALgBFAFgARQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
private System.IO.Stream GetBinaryDataStream(string base64String)
{
return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
#endregion
Alternate Approach [I'm not sure, you're interested :)]: to due binary data part, code snippet for same using Interop. works perfectly!
Excel.Application xlApp = new Excel.Application();
Excel._Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:/Users/jeeva/sample-excel-files/SampleExcel.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
xlWorksheet.PageSetup.PrintTitleRows = "$1:$1";
xlWorkbook.Save();
xlWorkbook.Close();
Previous One:
These three lines not nesscary.
DefinedNames dn = new DefinedNames();
wbp.Workbook.Append(dn);
wbp.Workbook.Save();
To set Print Titles, DefinedName Text should be in format of "Worksheet_name!Row_Range or Column_Range".
For example: Row range: Worksheet1!$1:$1
and Column range: Worksheet1!$A:$A
using (SpreadsheetDocument xl = SpreadsheetDocument.Open(@"C:/Users/jeeva/sample-excel-files/SampleExcel.xlsx", true))
{
WorkbookPart wbp = xl.WorkbookPart;
// Input for this program
string name = "_xlnm.Print_Titles";
string comment = "this is Print Titles for Sheet1";
string text = "Sheet1!$1:$1";
if (wbp.Workbook.DefinedNames == null)
{
wbp.Workbook.DefinedNames = new DefinedNames();
}
bool bFound = false;
foreach (DefinedName d in wbp.Workbook.DefinedNames)
{
// if found overwrite existing defined name
if (d.Name.Value.Equals(name, StringComparison.InvariantCultureIgnoreCase))
{
bFound = true;
d.Text = text;
d.Comment = comment;
break;
}
}
if (!bFound) // if not found, add one
{
wbp.Workbook.DefinedNames.Append(getDefinedName(name, text, comment));
}
wbp.Workbook.Save();
xl.Close();
}
private static DefinedName getDefinedName(string Name, string Text, string Comment)
{
DefinedName dn = new DefinedName();
dn.Text = Text;
dn.Name = Name;
dn.Comment = Comment;
dn.LocalSheetId = (UInt32Value)1U;
dn.Xlm = true; // since its a special schema name
}
Below code block solved my problem:
string fileName = AppDomain.CurrentDomain.BaseDirectory + "/Content/Master-License-Export.xlsx";
var spreadsheetDocument = SpreadsheetDocument.Open(fileName, true);
WorkbookPart wbp = spreadsheetDocument.WorkbookPart;
UInt32 localSheetId = 0; //LocalSheetIds are 0-indexed.
var definedName = new DefinedName
{
Name = "_xlnm.Print_Titles",
LocalSheetId = localSheetId,
Text = String.Format("\'{0}\'!${1}:${2}", "Master-License-Export", 1, 4)
};
if (wbp.Workbook.DefinedNames == null)
{
var definedNamesCol = new DefinedNames();
wbp.Workbook.Append(definedNamesCol);
}
wbp.Workbook.DefinedNames.Append(definedName);
wbp.Workbook.Save();
spreadsheetDocument.Close();