打印时将行设置为重复 – 打开XML和Excel

我试图得到一个xlsx文件的前4行,在打印时在每个页面的顶部重复。 我正在使用Open XML SDK来完成此操作。

我的文件正在由SSRS执行Web服务生成为文件stream。 然后,我需要修改几个布局和页面设置(页边距,页面大小,缩放等),然后将文件传递给用户,因为我的用户有很小的Excel敏锐度。 我需要调整的所有其他设置工作正常,所以我已经从这个例子中删除了所有的代码。

当尝试使用下面的代码设置重复页面标题时,它会执行没有错误。 但是,当我尝试执行代码后打开文件,我得到一个损坏的文件消息,它不会打开。

任何想法我在这里做错了吗?! 这让我疯狂!

using (SpreadsheetDocument xl = SpreadsheetDocument.Open("C:\\" + filename, true)) { WorkbookPart wbp = xl.WorkbookPart; //----------------------------------------- // repeat rows at top when printing //----------------------------------------- DefinedNames dn = new DefinedNames(); wbp.Workbook.Append(dn); wbp.Workbook.Save(); DefinedNames definedNames = wbp.Workbook.Descendants<DefinedNames>().FirstOrDefault(); DefinedName n1 = new DefinedName() { Name = "_xlnm.Print_Titles", LocalSheetId = (UInt32Value)0U }; n1.Text = "alpha_sort_nc!$1:$4"; definedNames.Append(n1); wbp.Workbook.Save(); xl.Close(); } 

更新: @Duncan – 您的评论后,我试图找出文件损坏的根本原因。 它变成SpreadsheetPinterSettings是文件中的二进制数据。 即使将DefinedName _xlnm.Print_Titles添加到电子表格中, 它在打印/打印预览中没有任何效果。

看来用于处理电子表格打印机设置的Open XML方法没有帮助。

从Open XML SDK生产力工具反映代码:在MS Excel中手动创build打印标题

 // 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/AP/qCm8IAAD/////////////////////wAAAAAAAAAADwAAAAABAAAPAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAA/////RVhDRUwuRVhFAAAAAAAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAAPAAAAAAEAAA="; private System.IO.Stream GetBinaryDataStream(string base64String) { return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String)); } #endregion 

替代方法[我不知道,你感兴趣:)]:到正确的二进制数据部分,代码片断使用Interop相同。 完美的作品!

 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(); 

前一个:

这三条线不是内ne的。

 DefinedNames dn = new DefinedNames(); wbp.Workbook.Append(dn); wbp.Workbook.Save(); 

要设置打印标题,DefinedName文本应该是“Worksheet_name!Row_Range或Column_Range”的格式。
例如:行范围: Worksheet1!$1:$1和列范围: 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 } 

这可以通过ClosedXML

 Worksheet.PageSetup.SetRowsToRepeatAtTop(1, 5);