SXSSFWorkbook 自适应宽度

发布时间 2023-12-22 13:58:15作者: atq
XSSFWorkbook wb = new XSSFWorkbook(new File(...));
swb = new SXSSFWorkbook(wb, 100);
SXSSFSheet sheet = swb.getSheetAt(0);

sheet.trackAllColumnsForAutoSizing();
int colSize =  swb.getXSSFWorkbook().getSheetAt(0).getRow(0).getLastCellNum();
for (int i = 0; i < colSize; i++) {
    //模板定义的最小宽度
     int width = sheet.getColumnWidth(i);
     sheet.autoSizeColumn(i);
    //新宽度
     int newWidth = sheet.getColumnWidth(i);
     if (width > newWidth) {
         sheet.setColumnWidth(i,width);
     }
}