[alibaba/easyexcel]使用inputStream作为模板时,报错

2024-05-16 542 views
8

建议先去看文档 快速开始常见问题 触发场景描述 使用模板文件生成excel文件,使用流(InputStream)作为模板,则报错(错误见异常)

经过分析,可能的原因 文件流是xls作为模板的,源码中在初始化excelType的时候,对inputStream做为模板的情况做处理未处理,默认成了xlsx。请大佬确认下这个需要修复吗? image 目前解决方法,自己拿到inputStream之后判断是xls还是xlsx,在设置excelType。 ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(templateFile).excelType(ExcelTypeEnum.XLSX).build();

触发Bug的代码

    FileOutputStream outputStream = new FileOutputStream("test11.xls");
    ClassPathResource classPathResource = new ClassPathResource("template/test.xls");
    InputStream templateFile = classPathResource.getInputStream();
    ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(templateFile).build();

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

com.alibaba.excel.exception.ExcelGenerateException: Create workbook failure
    at com.alibaba.excel.context.WriteContextImpl.<init>(WriteContextImpl.java:87)
    at com.alibaba.excel.write.ExcelBuilderImpl.<init>(ExcelBuilderImpl.java:35)
    at com.alibaba.excel.ExcelWriter.<init>(ExcelWriter.java:47)
    at com.alibaba.excel.write.builder.ExcelWriterBuilder.build(ExcelWriterBuilder.java:130)
    at xxx.xxx.xxx.SettlementBillService.exportBill(SettlementBillService.java:219)
    at xxx.xxx.xxx.SettlementBillService$$FastClassBySpringCGLIB$$d057b4a3.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
    at xxx.xxx.xxx.SettlementBillService$$EnhancerBySpringCGLIB$$c54f3362.exportBill(<generated>)
    at xxx.xxx.xxx.SettlementBillServiceTest.exportBill(SettlementBillServiceTest.java:25)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    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.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:325)
    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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    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.run(ParentRunner.java:363)
    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:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179)
    at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:98)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:295)
    at com.alibaba.excel.util.WorkBookUtil.createWorkBook(WorkBookUtil.java:32)
    at com.alibaba.excel.context.WriteContextImpl.<init>(WriteContextImpl.java:85)
    ... 39 more

回答

0

请问有人正在处理这个问题吗,我想在最近的几个月内尝试处理这个bug @zhuangjiaju

7

这个需要自己判断。

2

easyexcel 版本 :2.2.10;解决办法:填充模板时只能使用.xlsx结尾的文件!!,一定要用xlsx结尾的文件去填充