导出Excel表格的时候,如果数据过多的话,会出现异常情况
The maximum number of Cell Styles was exceeded. You can define up to 64000 style in a .xlsx Workbook
原代码:(创建多个cellStyle对象导致出现的问题)
public class SalesOrderProductProcessesV2CellWriteHandler implements CellWriteHandler {
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
List<WriteCellData<?>> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead){
if (Arrays.asList("A", "B", "C", "D", "空白").contains(writeSheetHolder.getSheet().getSheetName())){
CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
//设置上下左右边框
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
//文字上下左右居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellStyle(cellStyle);
}
}
}
优化处理后:(同样的样式可以创建一个就可以了,重复调用)
public class SalesOrderProductProcessesV2CellWriteHandler implements CellWriteHandler {
/**
* 样式列表
*/
private Map<String, CellStyle> styles;
/**
* 获取默认的表格样式对象
*/
private CellStyle getDefaultStyles(Workbook wb) {
if (styles != null && styles.containsKey("default")) {
return styles.get("default");
} else {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
//设置上下左右边框
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
//文字上下左右居中
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
styles.put("default", style);
this.styles = styles;
return style;
}
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
List<WriteCellData<?>> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead){
if (Arrays.asList("A", "B", "C", "D", "空白").contains(writeSheetHolder.getSheet().getSheetName())){
CellStyle cellStyle = getDefaultStyles(writeSheetHolder.getSheet().getWorkbook());
cell.setCellStyle(cellStyle);
}
}
}