Set Rows to repeat at top when printing - Open XML and Excel

前端 未结 3 939
一个人的身影
一个人的身影 2021-01-20 03:05

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

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 04:10

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

提交回复
热议问题