之前使用的3.9版本,autoSizeColumn方法一切正常,现在切换到了3.15版本这个方法就出先了问题,问题如下,无法自动追踪所有的列。
Exception in thread "main" java.lang.IllegalStateException: Could not auto-size column. Make sure the column was tracked prior to auto-sizing the column. at org.apache.poi.xssf.streaming.SXSSFSheet.autoSizeColumn(SXSSFSheet.java:1606) at org.apache.poi.xssf.streaming.SXSSFSheet.autoSizeColumn(SXSSFSheet.java:1560) at com.thinkgem.jeesite.common.utils.excel.ExportExcel.initialize(ExportExcel.java:224) at com.thinkgem.jeesite.common.utils.excel.ExportExcel.<init>(ExportExcel.java:181) at com.thinkgem.jeesite.common.utils.excel.ExportExcel.main(ExportExcel.java:474) Caused by: java.lang.IllegalStateException: Cannot get best fit column width on untracked column 0. Either explicitly track the column or track all columns. at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.getBestFitColumnWidth(AutoSizeColumnTracker.java:278) at org.apache.poi.xssf.streaming.SXSSFSheet.autoSizeColumn(SXSSFSheet.java:1603) ... 4 more Caused by: java.lang.IllegalStateException: Column was never explicitly tracked and isAllColumnsTracked() is false (trackAllColumns() was never called or untrackAllColumns() was called after trackAllColumns() was called). at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.getBestFitColumnWidth(AutoSizeColumnTracker.java:275) ... 5 more
解决如下:
1.之前使用的是sheet接口,改为SXSSFSheet
2.在autoSizeColumn前使用sheet.trackAllColumnsForAutoSizing();,手动设置。