So I am trying to add horizontal page breaks after every 11 rows of my Excel worksheet created and populated using Interop. Excel but have been so far unsuccessful in doing so. Here is my code snippet:
ws.HPageBreaks.Add(ws.Range["A11","D11"]);//ws=wb. Worksheets[1]
but it isn't working. Any advice is appreciated.
Update:
So after debugging and digging in the issue at hand, and using ExcelApp.Visible = true; , I have come to a very weird conclusion. The Horizontal Page Breaks are indeed being added at the intended locations in the worksheet using ws.HPageBreaks.Add(ws.Range) but upon saving and reopening the worksheet,the pagebreaks donot appear!
I think D11 has no effect there. You might add the breaks in a loop. ie:
PS: Parameter is "Before", therefore it is before 12.
EDIT: A full sample code:
This is the partial result:
EDIT2: This is just to show the code is working:
Print preview after opening the saved file