使用easyexcel读取Excel文件时报错(避坑)
使⽤easyexcel读取Excel⽂件时报错(避坑)
在项⽬开发中要求对Excel进⾏解析,获取⾥⾯的数据,本想着⽤easyexcel读取那还不是so easy嘛,⼏⾏代码搞定,结果发现项⽬启动的时候报错了
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'abstractIndexAO': Invocation of init method failed; nested exception is l.exception.ExcelAnalysisException: org.apache.poi.openxml4j.e  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.ateBean(Abstr
actAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.Object(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at t.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at t.fresh(AbstractApplicationContext.java:543)
at org.t.fresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.fresh(SpringApplication.java:693)
at org.springframework.freshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at com.banksteel.boot.Main.run(Main.java:63)
at com.banksteel.bone.BootApp.main(BootApp.java:23)
Caused by: l.exception.ExcelAnalysisException: org.apache.ptions.InvalidFormatException: Package should contain a content type part [M1.13]
at l.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:51)
at l.ExcelReader.<init>(ExcelReader.java:143)
at l.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:270)
at l.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:293)
at l.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:281)
steel.oilchem.appsupport.ao.impl.IndexAOImpl.init(IndexAOImpl.java:84)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
... 17 common frames omitted
Caused by: org.apache.ptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.openxml4j.PartsImpl(ZipPackage.java:292)
at org.apache.poi.openxml4j.Parts(OPCPackage.java:756)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
at l.analysis.adOpcPackage(XlsxSaxAnalyser.java:141)
at l.analysis.v07.XlsxSaxAnalyser.<init>(XlsxSaxAnalyser.java:58)
excel打不开怎么办at l.analysis.ExcelAnalyserImpl.choiceExcelExecutor(ExcelAnalyserImpl.java:92)
at l.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:45)
... 29 common frames omitted
  本着⾯向百度编程的思想,⽹上⼀顿搜索,发现都是说我在解析 .xls、.xlsx⽂件时⽤错了对象。
2007版excel和2003版excel⽂件格式不同导致:
HSSFWorkbook & HSSFSheet :⽀持 xls 格式;
XSSFWorkbook & XSSFSheet :⽀持 xlsx 格式。
还以为是和别⼈使⽤的Excel版本不同,导致⽂件损坏,还特意去另存⼀份 .xlsx的⽂件,结果还是不⾏ 
忍⼀时越想越⽓,退⼀步越想越亏。我⽤的easyExcel啊,它会根据Excel的类型⾃动创建底层的解析对象,这不对啊
debug⼀下,发现它读取的是编译后target->classes⽬录下的Excel⽂件,双击发现⽂件损坏打不开,
⽼铁,这不对劲啊
凭什么我在resources⽬录下能正常打开,编译了之后就不⾏了啊。
突然意识到,会不会是在打包编译的过程中把Excel⽂件损坏了,导致⽂件读取失败,报错
本着⾯向百度编程的思想,⽹上⼀顿搜索,哎,别说,还真是啊
项⽬在编译或者打包过程中,会对excel进⾏编译,这样经过⽂件filter过滤⼀遍之后,excel⽂件就损坏了
  知道原因后就很好解决啦,在pom⽂件中排除Excel的过滤就⾏了啊
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
  然后在⽤maven plugin clean⼀下,再去运⾏,果然问题解决,再看target⽬录下的Excel⽂件,完好⽆损。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。