Problem fitting all columns on one page

Hi Aspose

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);

Am I doing something wrong, or is this a bug?

Kind Regards
Kenneth VIndum

@kvin-1

Thanks for using Aspose APIs.

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);
1 Like

Hi Shakeel

That did the trick. It doesn’t seem logical that setFitToPagesWide affects the vertical axis though…

Thank you for providing a quick solution,

Kind regards
Kenneth

@kvin-1

Thanks for using Aspose APIs.

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.

@kvin-1

The default value of FitToPagesWide and FitToPagesTall is 1, same as

Microsoft Excel > PageSetup

Please see the following screenshot.

When you only set FitToPagesWide, the FitToPagesTall will keep its default value 1.

screenshot.png (6.1 KB)

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.

image.png (23.4 KB)

@kvin-1

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.

@kvin-1

FYI:

We have added a comment on PageSetup.FitToPagesWide property.

/// <remarks> 
/// You have to set FitToPagesTall as zero if you want to fit all columns on one page. 
/// </remarks>  

Besides, we will add a new more helpful method PageSetup.SetFitToPages(int wide, int tall) for users.

Thats good news :slight_smile: Thank you for your help. Have a nice weekend.

The issues you have found earlier (filed as CELLSNET-45622) have been fixed in Aspose.Cells for .NET 17.9.