Excel POI设置自适应宽度
/** * 自适应列宽 * @param sheet * @param columnLength 列数 */ private static void setSizeColumn(HSSFSheet sheet, int columnLength) { for (int columnNum = 0; columnNum <= columnLength; columnNum++) { int columnWidth = sheet.getColumnWidth(columnNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; // 当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(columnNum) != null) { HSSFCell currentCell = currentRow.getCell(columnNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } sheet.setColumnWidth(columnNum, columnWidth * 256); } }
注:HSSFCell.CELL_TYPE_STRING若过期,则使用CellType.STRING
参考链接:https://blog.csdn.net/taisuiyu6397/article/details/107490858

浙公网安备 33010602011771号