Worksheet.autoFitColumns method appears to do nothing

I am looping over each worksheet in a workbook, trying to auto fit all columns on the worksheet. The autoFitColumns method does not appear to be auto fitting anything. Here is my code. Does this look inaccurate? This is the Java component, running on Linux.



package managers.file;


import java.io.*;
import com.aspose.cells.*;


public class ExcelManager {
private License m_license;
private ByteArrayInputStream m_file_data_native;
public ExcelManager(ByteArrayInputStream file_data_native) throws Exception {
// store input file data
this.m_file_data_native = file_data_native;
FileInputStream license_stream = new FileInputStream("/Users/ryan/Documents/github/java-converter/bin/lib/Aspose.Total.Java.lic");

// set aspose license
m_license = new License();
m_license.setLicense(license_stream);
}
public byte[] GetPDF() throws Exception {
// open workbook
Workbook workbook = new Workbook(this.m_file_data_native);
// set font path
CellsHelper.setFontDir("/usr/share/fonts/truetype/msttcorefonts");
// get all of the worksheets
WorksheetCollection worksheets = workbook.getWorksheets();
// print all sheet names
for (int x = 0; x < worksheets.getCount(); x++) {
// get worksheet object
Worksheet worksheet = worksheets.get(x);
// make this worksheet visible
worksheet.setVisible(true);
// auto fit columns
worksheet.autoFitColumns();
worksheet.autoFitRows();
// page setup options
PageSetup pagesetup = worksheet.getPageSetup();
pagesetup.setFitToPagesWide(1);
}
// save pdf to byte array stream
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.save(output, FileFormatType.PDF);
return output.toByteArray();
}
}

Hi,

Please download and try the latest version:

Aspose.Cells for Java 7.1.1
and see if it works fine.

If the problem still occurs, then please provide us your source xls/xlsx and output xls/xlsx files.

We will look into it and help you asap.

Thanks for the response. I am using Cells 7.1.1. I downloaded it maybe a week ago. I have attached the source file, and output file.

Hi,

I have tested your file with the latest version and found it is working fine.

Please see the code below and the output file.

Please make sure you are using all the latest version of prerequistes jars needed by aspose.cells jar file, you can find them in the latest zip:

Aspose.Cells for Java 7.1.1


Java


String filePath = “F:\Shak-Data-RW\Downloads\TermFilePN_20120204-20120210_Report.xls”;


Workbook workbook = new Workbook(filePath);

Worksheet sheet = workbook.getWorksheets().get(0);


sheet.autoFitColumns();

sheet.autoFitRows();


sheet.getPageSetup().setFitToPagesWide(1);

workbook.save(filePath+ “.out.pdf”);



Alright. I can see that it is applying the auto fit column width but I don’t think the auto fit width makes the columns wide enough. I feel like the C# component does a better version of this. Is it possible that these are not quite doing auto fit width in the same way?

Hi,

You can also adjust width according to your choice.

Please use sheet.getCells().setColumnWidth() method to set the width as you want.

The better approach will be that you first auto fit columns, then read the width of your column using getColumnWidth, then after adding some value inside it, set it using setColumnWidth.

I had thought of that. I am just honestly not convinced that autofit is working as intended. Look at this resulting PDF. I applied autofit to these columns. I don’t think any of these columns look like they’ve been autofit well.

Hi,

Thanks for your feedback.

I have forwarded your issue to development team and logged this issue in our database.

We will look into it and get back to you asap.

This issue has been logged as CELLSJAVA-40142.

Hi,

We have made java version use the same logic with .net version for autoFitColumn() methods. So please try this new fix aspose-cells-7.1.1.4-java to check whether it can give you better result.

It definitely looks better, in my opinion. I still have a sheet that results in a few #'s, because the column didn’t get wide enough, but I’m not that picky. This has at least made it so that it looks like auto-fit is actually being applied, whereas the previous version did not look like it.


Thank you.

Hi,


Could you attach some some sample file(s) and also highlight the problematic cells (encircling with red color), we will log and evaluate your issue further.

Thank you.

The issues you have found earlier (filed as CELLSJAVA-40142) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.