easyexcel 自适应(行宽, 行高)

注解类

 @ColumnWidth(value = 20) 宽度 value:宽度大小   
 @ContentRowHeight(int):设置 row 高度,不包含表头标记在 类上
 @HeadRowHeight(int):设置表头高度(与 @ContentRowHeight 相反)标记在类上
 @ColumnWidth(int):设置列宽标记在属性上

代码类

  • 自适应宽度
public class CustomCellWriteWeightConfig extends AbstractColumnWidthStyleStrategy {
    private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (needSetWidth) {
            Map<Integer, Integer> 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<CellData> 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;
                }
            }
        }
    }
}
  • 自适应行高
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<Cell> 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));
    }
}
  • 注册器处理
EasyExcel.registerWriteHandler(new CustomCellWriteWeightConfig())
         .registerWriteHandler(new CustomCellWriteHeightConfig());
  • 12
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
EasyExcel中实现自适应行高度的方法是通过设置单元格样式来实现的。下面是一种实现自适应行高度的方法: 1. 首先,导入EasyExcel的相关包: ```java import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; ``` 2. 创建一个实现自适应行高度的方法: ```java public static void autoAdjustRowHeight(List<List<String>> data, String filePath) { // 创建Excel写入器 ExcelWriterBuilder writerBuilder = EasyExcel.write(filePath); // 创建Sheet写入器 ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet(); // 遍历数据列表,逐行写入数据 for (List<String> rowData : data) { // 写入一行数据 sheetBuilder.row(rowData); // 设置最小行高 sheetBuilder.rowHeight(-1, 20); // 可根据需要调整最小行高 // 设置自动调整行高 sheetBuilder.autoRowHeight(true); } // 完成写入操作 sheetBuilder.doWrite(); } ``` 3. 调用该方法实现自适应行高度: ```java public static void main(String[] args) { // 准备数据 List<List<String>> data = new ArrayList<>(); data.add(Arrays.asList("姓名", "年龄", "地址")); data.add(Arrays.asList("张三", "18", "北京市朝阳区")); data.add(Arrays.asList("李四", "20", "上海市浦东新区")); // 调用自适应行高度方法 autoAdjustRowHeight(data, "output.xlsx"); } ``` 以上代码会生成一个名为"output.xlsx"的Excel文件,并将数据写入该文件,同时自动适应行高度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值