Using version 3.7 XSSFSheet.autoSizeColumn calculates the size of a large column beyond the 255 character maximum. The HSSFSheet.autoSizeColumn does not do this. I noticed that bug 50211 combined the XSSF and HSSF autoSizeColumn methods. I have not tried the latest developer release; however, I wanted to get this in there to make sure this bug isn't carried over from the XSSF class. To reproduce: import org.apache.poi.xssf.usermodel.*; import java.io.*; public class AutoSizeTest { public static void main(String args[]) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(""); XSSFRow row = sheet.createRow(0); XSSFCell cell0 = row.createCell(0); cell0.setCellValue("www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com"); sheet.autoSizeColumn(0); sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // FAILS HERE try { FileOutputStream out = new FileOutputStream("working.xls"); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } // main }
Please re-test with a recent svn checkout / nightly build and confirm if the problem is there or not.
I do not have available time in the near future to test with the latest code base. If anyone has time available, please test using the code provided. If there is a jar that I can just drop into my application, I would probably have the fifteen minutes to try that within the next few weeks just let me know where to find it.
I have tested the above code in 3.8 beta 3 and it failed. There is also a regression for HSSF Sheets. The following code works in POI 3.7, but fails in POI 3.8 beta3. package org.apache.poi.ss.examples; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.xssf.usermodel.*; import java.io.*; public class AutoSizeTest { public static void main(String args[]) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Sheet1"); HSSFRow row = sheet.createRow(0); HSSFCell cell0 = row.createCell(0); String longValue = "www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, " + "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com"; cell0.setCellValue(longValue); sheet.autoSizeColumn(0); sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // FAILS HERE try { FileOutputStream out = new FileOutputStream("C:/workspace/working.xls"); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } } // main }
Fixed in r1136330, junit added. Yegor