🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
设置列宽可以有如下几种方式。 [TOC] # 1. @ColumnWidth设置 ```java @Data @AllArgsConstructor public class Dog { @ColumnWidth(25) @ExcelProperty("名字") private String name; @ColumnWidth(50) @ExcelProperty("年龄") private Integer age; } ``` # 2. 拦截器设置 **1. 继承AbstractColumnWidthStyleStrategy** ```java public class ColumnWidthStyleHandler extends AbstractColumnWidthStyleStrategy { /** * 每写入一列就被调用一次 */ @Override public void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList , Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { Sheet sheet = writeSheetHolder.getSheet(); //void setColumnWidth(int var1, int var2) //var1:单元格索引 //var2:单元格宽度 sheet.setColumnWidth(cell.getColumnIndex(), 6000); } } ``` **2. 注册拦截器** ```java @Test public void write02() { String path = "e:/upload/easyexcel/006.xlsx"; List<Cat> catList = Lists.newArrayList(); Cat cat01 = new Cat("小猫", 25); Cat cat02 = new Cat("橘猫", 30); catList.add(cat01); catList.add(cat02); EasyExcel.write(path, Cat.class) //注册拦截器 .registerWriteHandler(new ColumnWidthStyleHandler()) .sheet("cat1").doWrite(catList); } ```