[alibaba/easyexcel]简单的写入excel报错: Can not close IO.

2024-04-25 618 views
0
建议先去看文档

快速开始常见问题

触发场景描述

再SpringBoot UnitTest中测试easyexcel写入sheet时触发异常. easyexcel版本: 3.2.1

触发Bug的代码
   @Test
    public void genExcelTest() {
        Date startDate = DateTime.now().minusDays(20).toDate();
        List<AlertDailySummary> summaryList = alertMessageRecordService.selectSummaryForDaily("all"), startDate);
        Assert.assertNotNull(summaryList);
        String fileName = "D:\\12.xlsx";
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName, AlertSummaryHeadData.class).build();
            WriteSheet sheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(summaryList, sheet);
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
提示的异常或者没有达到的效果

com.alibaba.excel.exception.ExcelGenerateException: Can not close IO. at com.alibaba.excel.context.WriteContextImpl.finish(WriteContextImpl.java:420) at com.alibaba.excel.write.ExcelBuilderImpl.finish(ExcelBuilderImpl.java:99) at com.alibaba.excel.write.ExcelBuilderImpl.finishOnException(ExcelBuilderImpl.java:93) at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:64) at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:73) at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:50) at com.zeekr.platform.alert.platform.service.business.AlertDailySummaryJobTest.genExcelTest(AlertDailySummaryJobTest.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) Caused by: java.lang.ExceptionInInitializerError at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.setFeature(TransformerFactoryImpl.java:514) at org.apache.poi.util.XMLHelper.trySet(XMLHelper.java:265) at org.apache.poi.util.XMLHelper.getTransformerFactory(XMLHelper.java:219) at org.apache.poi.util.XMLHelper.newTransformer(XMLHelper.java:226) at org.apache.poi.openxml4j.opc.StreamHelper.saveXmlInStream(StreamHelper.java:56) at org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager.saveImpl(ZipContentTypeManager.java:69) at org.apache.poi.openxml4j.opc.internal.ContentTypeManager.save(ContentTypeManager.java:452) at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:520) at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1505) at org.apache.poi.openxml4j.opc.OPCPackage.close(OPCPackage.java:460) at org.apache.poi.ooxml.POIXMLDocument.close(POIXMLDocument.java:204) at org.apache.poi.xssf.usermodel.XSSFWorkbook.close(XSSFWorkbook.java:594) at org.apache.poi.xssf.streaming.SXSSFWorkbook.close(SXSSFWorkbook.java:935) at com.alibaba.excel.context.WriteContextImpl.finish(WriteContextImpl.java:383) ... 38 more Caused by: java.lang.NullPointerException at com.sun.org.apache.xalan.internal.XalanConstants.isJavaVersionAtLeast(XalanConstants.java:259) at com.sun.org.apache.xalan.internal.XalanConstants.(XalanConstants.java:243) ... 52 more 大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

回答

9

java.lang.NullPointerException 空指针了 你这个alertMessageRecordService.selectSummaryForDaily方法是不是有问题 Writer建议放在try with resource中

1

alertMessageRecordService.selectSummaryForDaily 这个请您相信我们,alertMessageRecordService.selectSummaryForDaily() 这个是没问题。目前怀疑是UnitTest和easyexcel哪里有兼容问题 。但是找不到具体

8

那你提供完整代码让我可以复现报错。 我把你上面的代码替换成简单数据是正确执行的

5

java.lang.NullPointerException 空指针了 你这个alertMessageRecordService.selectSummaryForDaily方法是不是有问题 Writer建议放在try with resource中 这个请您相信我们,alertMessageRecordService.selectSummaryForDaily() 这个是没问题。目前怀疑是UnitTest和easyexcel哪里有兼容问题 。但是找不到具体 怀疑是spring-boot-starter-test以boot方式启动单测时,和easyexcel结合有问题 但是你们的@Test不是以boot方式启动的

0

那你提供完整代码让我可以复现报错。 我把你上面的代码替换成简单数据是正确执行的

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("local") @ComponentScan(basePackages = {"com.alibaba.platform.alert.platform.service"}, lazyInit = true) @RunWith(SpringRunner.class) public abstract class BaseTest {

/**
 * 环境初始化
 */
@Before
public void initEnv() {
    Properties props = new Properties();
    props.setProperty("java.io.tmpdir", "D:\\");
    System.setProperties(props);
}

}

这个是spring-boot-starter-test的boot单测配置

4

。。。 AlertDailySummary 这是什么类 你的数据是什么,我不关心你的测试环境。 我要一份能跑的demo

2

。。。 AlertDailySummary 这是什么类 你的数据是什么,我不关心你的测试环境。 我要一份能跑的demo

你要关心的,我测试下来 这个方法不用动 把测试环境那段去掉 就正常了

6

easyexcel是工具 跟springboot没关系,你集成什么环境解决问题是你自己的事