easyexcel怎么设置自动列宽
时间: 2023-10-19 17:08:10 浏览: 208
首先需要引入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);
}
```
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)