6
简单写入, 指定拦截器, 修改标题样式. 无效
触发Bug的代码
public class Demo {
@Data
static class DemoModel {
@ExcelProperty(value = "数字标题", index = 0)
private Integer number;
@ExcelProperty(value = "字符串标题", index = 1)
private String str;
}
static class Demo1Handler extends AbstractCellWriteHandler {
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 把字符串标题那一列的内容变为红色(正常)
if (!isHead && cell.getColumnIndex() == 1) {
XSSFCellStyle xssfCellStyle = (XSSFCellStyle) writeSheetHolder.getSheet().getWorkbook().createCellStyle();
Font font = writeSheetHolder.getSheet().getWorkbook().createFont();
font.setColor(Font.COLOR_RED);
xssfCellStyle.setFont(font);
cell.setCellStyle(xssfCellStyle);
}
}
}
static class Demo2Handler extends AbstractCellWriteHandler {
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
// 把字符串标题变为红色(无效)
if (isHead && cell.getColumnIndex() == 1) {
XSSFCellStyle xssfCellStyle = (XSSFCellStyle) writeSheetHolder.getSheet().getWorkbook().createCellStyle();
Font font = writeSheetHolder.getSheet().getWorkbook().createFont();
font.setColor(Font.COLOR_RED);
xssfCellStyle.setFont(font);
cell.setCellStyle(xssfCellStyle);
}
}
}
@Test
public void fun() throws Exception {
String data = "[{\"number\":1,\"str\":\"字符串内容1\"},{\"number\":2,\"str\":\"字符串内容2\"}]";
ArrayList<DemoModel> listData = JSON.parseObject(data, new TypeReference<ArrayList<DemoModel>>() {
});
EasyExcel.write(new FileOutputStream("D:\\demo1.xlsx"), DemoModel.class)
.registerWriteHandler(new Demo1Handler())
.sheet()
.doWrite(listData);
EasyExcel.write(new FileOutputStream("D:\\demo2.xlsx"), DemoModel.class)
.registerWriteHandler(new Demo2Handler())
.sheet()
.doWrite(listData);
}
}
提示的异常或者没有达到的效果
标题样式仍然是默认样式. 拦截器修改的样式不生效