Bug 47581

Summary: XSSFSheet.setColumnWidth does not do anything
Product: POI Reporter: David Agnew <prescindor>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.5-dev   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X 10.4   
Attachments: Test case with code to generate good and bad files.

Description David Agnew 2009-07-25 21:05:47 UTC
After being dissatisfied with the results of XSSFSheet.autoSizeColumn, I tried to use XSSFSheet.setColumnWidth to implement my own crude attempt at column width fitting. Nothing happened. The columns in the output .xlsx were all the default width.

Armed with the knowledge that autoSizeColumn was able to do something, if not what I wanted, but setColumnWidth was not, I looked at the source. Turns out autoSizeColumn calls columnHelper.setColBestFit and columnHelper.setCustomWidth when it sets the width it computed, whereas setColumnWidth does not.

Workaround: I was able to set my computed widths by calling the columnHelper methods prior to the setColumnWidth call (easy to do from JRuby — from Java, you probably need a reflection hack). Actually, it looks like you only need either one of the columnHelper calls, not both.
Comment 1 Yegor Kozlov 2009-08-05 10:15:55 UTC
Can you attach a sample code that demonstrates the problem? 

The following code works fine to me:

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sh = wb.createSheet();
        sh.setColumnWidth(0, 256*5);  //5-character wide
        sh.setColumnWidth(1, 256*20); //20-character wide
        sh.setColumnWidth(2, 256*50); //50-character wide

        FileOutputStream out = new FileOutputStream("bug-47581.xlsx");
        wb.write(out);
        out.close();

I tested with current trunk and Excel 2007.

Yegor
Comment 2 David Agnew 2009-09-08 09:42:33 UTC
Created attachment 24228 [details]
Test case with code to generate good and bad files.

Attached is a Java app that generates good and bad test files.

The bad version of the file displays incorrectly in Excel 2008 for Mac 12.2.1 (090605) but correctly in the Mac Finder's preview display and correctly on Windows, using Excel 2007.

The good version, produced using the workaround (calling ColumnHelper.setCustomWidth) displays correctly in Excel Mac 2008. The good version is a few bytes longer.
Comment 3 David Fisher 2009-09-08 09:51:44 UTC
You should upgrade your Mac Excel version.

Please see https://issues.apache.org/bugzilla/show_bug.cgi?id=47559

Microsoft was very responsive and quick with the patch. It was delivered about one month ago.
Comment 4 David Agnew 2009-09-08 10:03:23 UTC
(In reply to comment #3)
> You should upgrade your Mac Excel version.
> 
> Please see https://issues.apache.org/bugzilla/show_bug.cgi?id=47559
> 
> Microsoft was very responsive and quick with the patch. It was delivered about
> one month ago.

I tested with 12.2.1. I just ran Check for Updates, and it didn't turn up any.
Comment 5 Yegor Kozlov 2009-09-13 10:53:02 UTC
I fixed XSSFSheet.setColumnWidth to always set the customWidth="true" attribute of the CTCol bean. 

The fix was committed in r814358

Regards,
Yegor