.NET EPPlus OpenXml - Repeat Excel row on every page
Asked Answered
O

2

6

I'm trying to maintain a row (header fields) on every Excel page. I'm using OpenXML contained in the EPPlus libraries.

I've tried following line of code:

myWorkSheet.PrinterSettings.RepeatRows = New ExcelAddress(String.Format("${0}:${0}", firstRowNummerExcel.ToString()))

This doesn't error or crash on me, but just doesn't do anything in particular...

The PrinterSettings object does seem to work, as executing following line of code works:

worksheetVertrokkenRunderen.PrinterSettings.ShowHeaders = true

This makes me think that perhaps the inserted ExcelAddress is incorrect?

Overmatter answered 18/3, 2014 at 13:15 Comment(0)
O
12

I think I found my own answer by setting a recurring row in Excel and then reading that document into memory and extra what is in the RepeatRows property: Apparently, you must add the name of the worksheet as well...

myWorkSheet.PrinterSettings.RepeatRows = New ExcelAddress(String.Format("'{1}'!${0}:${0}", firstRowNummerExcel, myWorkSheetName))

This works :-)

Overmatter answered 18/3, 2014 at 16:43 Comment(1)
I've since found a simpler way (if you're just trying to repeat the first row: myWorksheet.PrinterSettings.RepeatRows = new ExcelAddress("1:1");Velez
S
1

There's currently a bug in EPPlus where setting both the RepeatColumns and RepeatRows at the same time causes an invalid reference to get created and is ignored by Excel.

The workaround is to add the following Named range manually. (Which is exactly the same as EPPlus is trying to do.)

worksheet.Names.AddFormula("_xlnm.Print_Titles", $"'{worksheet.Name}'!$A:$B,'{worksheet.Name}'!$1:$4");
Specialism answered 29/5, 2017 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.