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.
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
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.
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.
(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.
I fixed XSSFSheet.setColumnWidth to always set the customWidth="true" attribute of the CTCol bean. The fix was committed in r814358 Regards, Yegor