Home » excel » How to know the page break in Excel using C#

How to know the page break in Excel using C#

Posted by: admin April 23, 2020 Leave a comment

Questions:

I´m using C# to create and format a Excel spreadsheet, so I need to format (merge cells, change the font, etc) until the final of the first page. How can I know the final line of the page in the Excel spreadsheet?

How to&Answers:

The following code will give you the last row number before every horizontal page break in an Excel worksheet:

foreach (Excel.HPageBreak pageBreak in worksheet.HPageBreaks)
{
    int row = pageBreak.Location.Row - 1;

    // ...
}