【原创】关于高版本poi autoSizeColumn方法异常的情况

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:00   2344   0

之前使用的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();,手动设置。

转载于:https://www.cnblogs.com/Yiran583/p/6739187.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP