easyexcel2.2.8 版本
package com.fofund.bus.zgt.core.biz.component.excel; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Workbook; @SuppressWarnings("rawtypes") public class CustomCellWriteHandler extends AbstractColumnWidthStyleStrategy { private Map<Integer, Map<Integer, Double>> CACHE; public CustomCellWriteHandler(){ this.CACHE = new HashMap<>(); } @Override protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { boolean needSetWidth = isHead || CollectionUtils.isNotEmpty(cellDataList); if (!needSetWidth) { return; } Map<Integer, Double> maxColumnWidthMap = CACHE.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<>(16)); double columnWidth = this.dataLength(cellDataList, cell, isHead); if (columnWidth >= 0) { if (columnWidth > 255) { columnWidth = 255; } Double maxColumnWidth = maxColumnWidthMap.getOrDefault(cell.getColumnIndex(), null); if (maxColumnWidth == null || columnWidth > maxColumnWidth) { maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth); writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), (int) (columnWidth * 255)); } } Workbook workbook = cell.getSheet().getWorkbook(); //设置数据格式索引 CellStyle cellStyle = workbook.createCellStyle(); // 设置列的格式为文本格式 cellStyle.setDataFormat((short)49); writeSheetHolder.getSheet().setDefaultColumnStyle(cell.getColumnIndex(), cellStyle); } private double dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) { if (isHead != null && isHead) { return this.getExcelWidth(cell.getStringCellValue()); } CellData cellData = cellDataList.get(0); CellDataTypeEnum type = cellData.getType(); if (type == null) { return 0; } else { switch (type) { case STRING: return this.getExcelWidth(cellData.getStringValue()); case BOOLEAN: return this.getExcelWidth(cellData.getBooleanValue().toString()); case NUMBER: return this.getExcelWidth(cellData.getNumberValue().toString()); default: return 0; } } } /** * 调整单元格字符字节宽度,easyExcel默认直接用的UTF-8的byte长度,导致一旦三字节的字符过多就会变得很宽,一字节的字符过多就会不够宽 */ private double getExcelWidth(String str) { double length = 0; char[] chars = str.toCharArray(); for (char c : chars) { byte[] bytes = this.getUtf8Bytes(c); if (bytes.length == 1) { length += 1.6; } if (bytes.length == 2) { length += 2.2; } if (bytes.length == 3) { length += 3.85; } if (bytes.length == 4) { length += 4.2; } } return length; } private byte[] getUtf8Bytes(char c) { char[] chars = {c}; CharBuffer charBuffer = CharBuffer.allocate(chars.length); charBuffer.put(chars); charBuffer.flip(); ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(charBuffer); return byteBuffer.array(); } }