easyexcel⾃适应列宽
在导出时注册registerWriteHandler(new CustomCellWriteHandler())
/**
* @author jamin
* @date 2020/7/29 11:28
* 设置⾃适应列宽
*/
public class CustomCellWriteHandler 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 = (SheetNo());
if(maxColumnWidthMap == null){
maxColumnWidthMap =new HashMap<>();
CACHE.SheetNo(), maxColumnWidthMap);
}
Integer columnWidth =this.dataLength(cellDataList, cell, isHead);
bigdecimal转换为integer
if(columnWidth >=0){
if(columnWidth >255){
columnWidth =255;
}
Integer maxColumnWidth = (ColumnIndex());
if(maxColumnWidth == null || columnWidth > maxColumnWidth){
maxColumnWidthMap.ColumnIndex(), columnWidth);
}
}
}
}
private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead){
if(isHead){
StringCellValue().getBytes().length;
}else{
CellData cellData = (0);
CellDataTypeEnum type = Type();
if(type == null){
return-1;
}else{
switch(type){
case STRING:
StringValue().getBytes().length;
case BOOLEAN:
BooleanValue().toString().getBytes().length;
case NUMBER:
NumberValue().toString().getBytes().length;
default:
return-1;
}
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。