0

I am using the NPOI framework to generate a 97/2003 Excel workbook. I need to set a page break every 44 rows and from the example provided in the framework download, the code to do this is:

sheet.SetRowBreak(int row)

I can verify these are setting a collection of row integers but when opening the document and viewing the Page Break preview, there is but a single page that encompasses the entire worksheet.

Sample Code below:

for(int rowCount = 0; rowCount < MaxRows; rowCount += 44)
{
   worksheet.SetRowBreak(rowCount);
}

Ideas?

2 Answers2

1

This is because of the default values from NPOI. It is set to fit the whole thing to one page. Just add this line to your code and you should have more than one page if the worksheet contains enough rows or columns.

worksheet.FitToPage = false;

But if you want to fit your worksheets width to one page don't change the FitToPage property, but add something like this to your code:

worksheet.PrintSetup.FitHeight = 9999;
// worksheet.PrintSetup.FitWidth = 1; // this is the default value

Then your worksheet always will have the width of one page or less and the height of 9999 pages or less.

0

Are you saving the file after you make the changes in POI?

I tried this, and it worked fine as long as the file was saved afterward:

for(int rowCount = 43; rowCount < sheet.LastRowNum; rowCount += 44)
{
    worksheet.SetRowBreak(rowCount);
}
Jay
  • 56,361
  • 10
  • 99
  • 123
  • I am saving the file but the page breaks were not showing there for some reason. Did you by chance make any changes to the PrintArea, margins, or papersize attributes in the workbook or worksheet? I believe this may have been the cause and fix of my issue but I haven't had time to really test it thoroughly. – RominNoodleSamurai Jul 08 '13 at 15:30
  • @RominNoodleSamurai No, I didn't make any changes to those properties. Notice that I did change the code a little bit. – Jay Jul 08 '13 at 15:33