Search code examples
c#excelnpoipage-break

NPOI excel page break get last row number


I create a Excel sheet with NPOI in a C#-program and I can loop to each row. For some rows I define HeightInPoints and I can do this for all rows.

The auto page break breaks not at the row I want, so Ihave to do this in code. How can I find out, which is the last row before autobreak?

I use only two formats to print A4 = 14.8 x 21 cm or letter = 8.5 x 11 inch. Is it possible to determine how many points used per page height? And so calculate the last row before page break?

Ottilie


Solution

  • I opened a blank excel document in Excel 2016 and checked the row height (just click on the border of the row as if to drag and it shows). It shows the height as "14.5 points (29 pixels)".

    I then went to View -> Page Layout and a grid appears with inches as scale. Now, I can see the row height as "0.2 inches (29 pixels)".

    So, this is the equation:

    1 default row = 0.2 inch = 14.5 points = 29 pixels.

    By doing the conversion, 1 inch is 72.5 points.

    Since you know the height of each row you are setting in points (HeightInPoints) and you know the size of A4 and letter, you can do the math. You may want to leave some space for border.

    I did the math for Letter (11 inches) with default row height and it comes to 55 rows and the print preview in excel with Letter size shows 47 rows because it leaves space for border, so it works!

    You can try and let us know if it works.