I’m unable to use the method sheet.getPageSetup().setFitToPagesWide(1) with the expected result. Calling this method causes the page to also fit all rows on the page. I only expected the columns to fit. Excel displays the setting “Fit Sheet on One Page” when calling the method. I believe the correct behavior would have been “Fit All Columns on One Page”.
The following code will generate a xls file with the described problem, and will fit all 200 rows in one page.
Workbook wb = new Workbook();
wb.getWorksheets().add();
Worksheet sheet = wb.getWorksheets().get(0);
Cells cells = sheet.getCells();
for (int i = 0; i < 200; i++) {
Cell cell = cells.get(i, 0);
cell.setValue(i);
}
sheet.getPageSetup().setFitToPagesWide(1); //fits one page wide
String filename = "C:\\temp\\test.xls";
new File(filename).getParentFile().mkdirs();
wb.save(filename, SaveFormat.EXCEL_97_TO_2003);
Please use these two lines and read the comments for your needs. It will fix your issue. Let us know your feedback.
Java
//Both of these lines are necessary to set
//Fit All Columns on One Page
sheet.getPageSetup().setFitToPagesWide(1);
sheet.getPageSetup().setFitToPagesTall(0);
You seem to be right so we have forwarded your message for product team investigation and advice. Once, we will have some news for you, we will let you know asap. Besides, it is good to know that the given piece of code has fixed the issue at hand. Let us know if you encounter any other issue, we will be glad to look into it and help you further.
Yes, I see your point. It works that way. I’ll try to explain. I’m looking in the print dialog.
Setting sheet.getPageSetup().setFitToPagesWide(1); will make the dialog choose “Fit Sheet on One Page”.
Setting both properties will make the dialog choose “Fit All Columns on One Page”.
This means that I have to explicitly set the height to 0 (or null i Excel) in order to get the expected result. This is a little confusing. Perhaps it could be solved by adding a note in the documentation somewhere.
You are right. May be, we need to add some overload or parameter to deal with this situation. However, I have again logged your comment for product team further investigation. We will evaluate it more and help you.