Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tableWrite写入多个table,每个table对应不同的实例,实例中添加样式注解,报错 #1202

Closed
brucezhang0 opened this issue Mar 30, 2020 · 2 comments
Assignees
Labels
bug Something isn't working pending verification This problem needs to be confirmed

Comments

@brucezhang0
Copy link

建议先去看文档
快速开始常见问题
触发场景描述
版本 2.2.0-beta2
使用tableWrite方式写入,两个table对应两个类DemoData1,DemoData2。DemoData1和DemoData2中有添加样式@headstyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 42)
java.lang.NullPointerException: null
at com.alibaba.excel.util.StyleUtil.buildDefaultCellStyle(StyleUtil.java:27)
at com.alibaba.excel.util.StyleUtil.buildHeadCellStyle(StyleUtil.java:49)
at com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy.setHeadCellStyle(AbstractVerticalCellStyleStrategy.java:45)
at com.alibaba.excel.write.style.AbstractCellStyleStrategy.afterCellDispose(AbstractCellStyleStrategy.java:56)
at com.alibaba.excel.util.WriteHandlerUtils.afterCellDispose(WriteHandlerUtils.java:159)

触发Bug的代码

   @Test
    public void tableWrite() {
        String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
        // 这里 需要指定写用哪个class去写
        ExcelWriter excelWriter = EasyExcel.write(fileName).build();
        // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
        // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
        WriteTable writeTable0 = EasyExcel.writerTable(0).head(, DemoData1.class).needHead(Boolean.TRUE).build();
        WriteTable writeTable1 = EasyExcel.writerTable(1).head(, DemoData2.class).needHead(Boolean.TRUE).build();
        // 第一次写入会创建头
        excelWriter.write(data(), writeSheet, writeTable0);
        // 第二次写如也会创建头,然后在第一次的后面写入数据
        excelWriter.write(data(), writeSheet, writeTable1);
        /// 千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

提示的异常或者没有达到的效果

@brucezhang0 brucezhang0 added the bug Something isn't working label Mar 30, 2020
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Apr 22, 2020
@zhuangjiaju
Copy link
Collaborator

这个问题原因在于 有个bug 拦截器在table的时候创建,被调用workbook的事件了。需要修改成,在table创建的 也要补充调用workbook的事件

@zhuangjiaju
Copy link
Collaborator

已经在2.2.1里面修复

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending verification This problem needs to be confirmed
Projects
None yet
Development

No branches or pull requests

2 participants