easyexcel怎么设置自动列宽
时间: 2023-10-19 12:08:10 浏览: 254
首先需要引入EasyExcel依赖,然后在对应的实体类上添加注解@ExcelProperty(value = "列名", index = 列索引),并在写入Excel时调用autoSizeColumn(列索引)方法即可设置该列自动列宽。
示例代码:
```java
public class DemoData {
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "年龄", index = 1)
private Integer age;
// getters/setters 省略
}
public void writeExcel() {
// 数据列表
List<DemoData> dataList = new ArrayList<>();
// 添加数据
// ...
// 写入Excel
String fileName = "demo.xlsx";
EasyExcel.write(fileName, DemoData.class)
.sheet("sheet1")
.doWrite(dataList);
// 设置自动列宽
Sheet sheet = EasyExcelFactory.read(fileName).getSheet(0);
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
}
```
阅读全文