EasyExcel-Customlize-Column-Width
- Easyexcel(1-注解介绍与使用)
- Easyexcel(2-文件 多Sheet、不同对象、多数据读取)
- Easyexcel(3-文件导出)
- Easyexcel(4-模板文件定义与导出)
- Easyexcel(5-自定义列宽)
- Easyexcel(6-单元格合并)
- Easyexcel(7-自定义样式)
- Easyexcel(8-通用工具类)
- Easyexcel(5-自定义列宽)
注解
@ColumnWidth
@Data
public class WidthAndHeightData {
@ExcelProperty("字符串标题")
private String string;
@ExcelProperty("日期标题")
private Date date;
@ColumnWidth(50)
@ExcelProperty("数字标题")
private Double doubleData;
}
注解使用时表头长度无法做到动态调整,只能固定设置,每次调整表头长度时只能重新修改代码
注意:@ColumnWidth 最大值只 能为255,超过255 * 256长度时会报错
查看XSSFSheet源码

类方法

AbstractHeadColumnWidthStyleStrategy
public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
// 判断是否满足 当前行索引不为空 && (当前是表头 || 当前行索引是首行)
// 如果不满足,则说明不是表头,不需要设置
boolean needSetWidth = relativeRowIndex != null && (isHead || relativeRowIndex == 0);
if (!needSetWidth) {
return;
}
Integer width = columnWidth(head, cell.getColumnIndex());
if (width != null) {
width = width * 256;
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), width);
}
}
protected abstract Integer columnWidth(Head head, Integer columnIndex);
}
通过继承 AbstractHeadColumnWidthStyleStrategy 类,实现 columnWidth 方法获取其对应列的宽度
SimpleColumnWidthStyleStrategy
源码查看
public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
private final Integer columnWidth;
public SimpleColumnWidthStyleStrategy(Integer columnWidth) {
this.columnWidth = columnWidth;
}
@Override
protected Integer columnWidth(Head head, Integer columnIndex) {
return columnWidth;
}
}