EasyExcel 文件导出 - 最终效果
具体的效果可通过修改代码来自行调整。经过调整后的样式与默认样式相比,美观程度大幅提升。
使用的 EasyExcel的版本
我使用的版本是4.0.0+。
设置表头样式和内容样式
首先创建一个写入处理器把表头和内容的样式传入进去,再使用registerWriteHandler进行注册。
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
excelWriter = EasyExcel.write(outputStream, clazz)
.registerWriteHandler(horizontalCellStyleStrategy)
.build();
上面的样式工具类。
public class ExcelStyleUtils {
public static WriteCellStyle getHeadStyle() {
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("宋体");
headWriteFont.setFontHeightInPoints((short) 13);
headWriteFont.setBold(true);
headWriteFont.setColor(IndexedColors.WHITE.getIndex());
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
headWriteCellStyle.setBottomBorderColor((short) 0);
headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
headWriteCellStyle.setLeftBorderColor((short) 0);
headWriteCellStyle.setBorderRight(BorderStyle.THIN);
headWriteCellStyle.setRightBorderColor((short) 0);
headWriteCellStyle.setBorderTop(BorderStyle.THIN);
headWriteCellStyle.setTopBorderColor((short) 0);
headWriteCellStyle.setWrapped(true);
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headWriteCellStyle.setShrinkToFit(true);
headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
return headWriteCellStyle;
}
public static WriteCellStyle getContentStyle() {
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 11);
contentWriteFont.setFontName("宋体");
contentWriteCellStyle.setWriteFont(contentWriteFont);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setBottomBorderColor((short) 0);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setLeftBorderColor((short) 0);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setRightBorderColor((short) 0);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setTopBorderColor((short) 0);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentWriteCellStyle.setWrapped(true);
contentWriteCellStyle.setShrinkToFit(true);
return contentWriteCellStyle;
}
}
设置自动列宽
下面的ExcelCellWidthStyleStrategy 这个类就是自定义实现自动列宽的类。
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());
ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
excelWriter = EasyExcel.write(outputStream, clazz)
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(widthStyleStrategy)
.build();
下面的MAX_COLUMN_WIDTH 参数根据自己的需求进行调整。
public class ExcelCellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
private static final int MAX_COLUMN_WIDTH = 50;
private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
boolean needSetWidth = isHead ||!CollectionUtils.isEmpty(cellDataList);
if (needSetWidth) {
Map<Integer, Integer> maxColumnWidthMap = (Map) CACHE.get(writeSheetHolder.getSheetNo());
if (maxColumnWidthMap == null) {
maxColumnWidthMap = new HashMap(16);
CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
}
Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
if (columnWidth >= 0) {
if (columnWidth > MAX_COLUMN_WIDTH) {
columnWidth = MAX_COLUMN_WIDTH;
}
Integer maxColumnWidth = (Integer) ((Map) maxColumnWidthMap).get(cell.getColumnIndex());
if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
((Map) maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);
writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
}
}
}
}
private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
if (isHead) {
return cell.getStringCellValue().getBytes().length;
} else {
CellData cellData = (CellData) cellDataList.get(0);
CellDataTypeEnum type = cellData.getType();
if (type == null) {
return -1;
} else {
switch (type) {
case STRING:
return cellData.getStringValue().getBytes().length;
case BOOLEAN:
return cellData.getBooleanValue().toString().getBytes().length;
case NUMBER:
return cellData.getNumberValue().toString().getBytes().length;
default:
return -1;
}
}
}
}
}