alibaba easyexcel 自适应(行宽, 行高) 您所在的位置:网站首页 excel如何设置单元格高度自适应 alibaba easyexcel 自适应(行宽, 行高)

alibaba easyexcel 自适应(行宽, 行高)

2024-07-16 07:54| 来源: 网络整理| 查看: 265

1. 简介

alibaba easyexcel 自适应,自适应表格的行宽与行高

2. 使用 2.1 依赖 com.alibaba easyexcel 2.2.10 2.2 自适应行宽 public class CustomCellWriteWeightConfig extends AbstractColumnWidthStyleStrategy { private Map CACHE = new HashMap(); @Override protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) { boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList); if (needSetWidth) { Map maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo()); if (maxColumnWidthMap == null) { maxColumnWidthMap = new HashMap(); CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); } Integer columnWidth = this.dataLength(cellDataList, cell, isHead); if (columnWidth >= 0) { if (columnWidth > 254) { columnWidth = 254; } Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex()); if (maxColumnWidth == null || columnWidth > maxColumnWidth) { maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth); Sheet sheet = writeSheetHolder.getSheet(); sheet.setColumnWidth(cell.getColumnIndex(), columnWidth * 256); } } } } /** * 计算长度 * @param cellDataList * @param cell * @param isHead * @return */ private Integer dataLength(List cellDataList, Cell cell, Boolean isHead) { if (isHead) { return cell.getStringCellValue().getBytes().length; } else { CellData cellData = cellDataList.get(0); CellDataTypeEnum type = cellData.getType(); if (type == null) { return -1; } else { switch (type) { case STRING: // 换行符(数据需要提前解析好) int index = cellData.getStringValue().indexOf("\n"); return index != -1 ? cellData.getStringValue().substring(0, index).getBytes().length + 1 : cellData.getStringValue().getBytes().length + 1; case BOOLEAN: return cellData.getBooleanValue().toString().getBytes().length; case NUMBER: return cellData.getNumberValue().toString().getBytes().length; default: return -1; } } } } } 2.2 自适应行高 public class CustomCellWriteHeightConfig extends AbstractRowHeightStyleStrategy { /** * 默认高度 */ private static final Integer DEFAULT_HEIGHT = 300; @Override protected void setHeadColumnHeight(Row row, int relativeRowIndex) { } @Override protected void setContentColumnHeight(Row row, int relativeRowIndex) { Iterator cellIterator = row.cellIterator(); if (!cellIterator.hasNext()) { return; } // 默认为 1行高度 Integer maxHeight = 1; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellTypeEnum()) { case STRING: if (cell.getStringCellValue().indexOf("\n") != -1) { int length = cell.getStringCellValue().split("\n").length; maxHeight = Math.max(maxHeight, length); } break; default: break; } } row.setHeight((short) (maxHeight * DEFAULT_HEIGHT)); } } 3. 注册处理器 EasyExcel.registerWriteHandler(new CustomCellWriteWeightConfig()) .registerWriteHandler(new CustomCellWriteHeightConfig());

注意:自适应行高中的数据必须有换行符(\n),并且导出的excel数据中必须聚焦行数据才会有换行效果(目前并不知道如何解决 - -)



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有