java.lang.IllegalStateException: The maximum number of Cell Styles was exceeded. You can define up to 64000 style in a .xlsx Workbook
public class CustomCellWriteHandler implements CellWriteHandler {
/**
* 设置拦截器顺序,需要 > 50000
*
* @return 拦截器顺序
*/
@Override
public int order() {
return 60000;
}
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
Cell cell = context.getCell();
if (BooleanUtils.isNotTrue(context.getHead())) {
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
// 此处代码有问题,每次进入条件,都会重复创建一个XSSFCellStyle对象
XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
}
}
public class CustomCellWriteHandler implements CellWriteHandler {
XSSFCellStyle cellStyle;
/**
* 设置拦截器顺序,需要 > 50000
*
* @return 拦截器顺序
*/
@Override
public int order() {
return 60000;
}
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
Cell cell = context.getCell();
if (BooleanUtils.isNotTrue(context.getHead())) {
Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
if (cellStyle == null) {
cellStyle = (XSSFCellStyle) workbook.createCellStyle();
}
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
}
}
在创建新样式之前,判断下是不是已经有这个样式了,没有的话再创建,这样就避免了重复创建样式对象导致的异常。