POI自适应列宽

2022-10-27  本文已影响0人  ShrJanLan
int templateColumnWidth = -1;
//最大列宽
int maximumColumnWidth = 15000;
for (int i = 0; i < cellCount; i++) {
    //获取原始列宽
    templateColumnWidth = sheet.getColumnWidth(i);
    //设置自适应列宽
    sheet.autoSizeColumn((short) i, true);
    int columnWidth = sheet.getColumnWidth(i);
    if (columnWidth > maximumColumnWidth) {
        //允许的最大列宽
        sheet.setColumnWidth(i, maximumColumnWidth);
    } else if (columnWidth < templateColumnWidth) {
        //允许的最小列宽(来自原始列宽)
        sheet.setColumnWidth(i, templateColumnWidth);
    }
}
上一篇下一篇

猜你喜欢

热点阅读