异常代码
// 缓存cellStyle
public class SalaryExportCellWriteHandler implements CellWriteHandler {
/**
* 导出需要设置单元格为百分数的字段
*/
private static List<String> percentCellTypeList = Lists.newArrayList("lastYearAprilSalaryRatio", "lastYearSpecialSalaryRatio", "thisYearQ1SalaryRatio", "basicAdjustRatio", "flexibleAdjustRatio", "recommendedAnnualSalaryIncreasedRatio");
private CellStyle cellStyle;
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (!isHead) {
if (Objects.isNull(cellStyle)) {
cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
}
// 设置百分比单元格格式
if (percentCellTypeList.contains(head.getFieldName())) {
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
} else {
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("General"));
}
cell.setCellStyle(cellStyle);
}
}
}
// 不缓存cellStyle 每次都创建
public class SalaryExportCellWriteHandler implements CellWriteHandler {
/**
* 导出需要设置单元格为百分数的字段
*/
private static List<String> percentCellTypeList = Lists.newArrayList("lastYearAprilSalaryRatio", "lastYearSpecialSalaryRatio", "thisYearQ1SalaryRatio", "basicAdjustRatio", "flexibleAdjustRatio", "recommendedAnnualSalaryIncreasedRatio");
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (!isHead) {
CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
// 设置百分比单元格格式
if (percentCellTypeList.contains(head.getFieldName())) {
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
} else {
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("General"));
}
cell.setCellStyle(cellStyle);
}
}
}
异常提示 请提供完整的异常提示,记住是全部异常! 建议描述 如果是按照上面的方式把cellStyle缓存起来的话设置的单元格格式无效,但是像下面这样每次都创建一个cellStyle是有效的,麻烦请教下是什么原因? ps: 上面那种debug的时候是进去的,但是就是无效