使用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 com.alibaba.excel.exception.ExcelAnalysisException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
	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.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(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: com.alibaba.excel.exception.ExcelAnalysisException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
	at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:51)
	at com.alibaba.excel.ExcelReader.<init>(ExcelReader.java:143)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:270)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:293)
	at com.alibaba.excel.read.builder.ExcelReaderBuilder.sheet(ExcelReaderBuilder.java:281)
	at com.mysteel.oilchem.appsupport.ao.impl.IndexAOImpl.init(IndexAOImpl.java:84)
	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.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.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
	at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:292)
	at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:756)
	at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
	at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.readOpcPackage(XlsxSaxAnalyser.java:141)
	at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.<init>(XlsxSaxAnalyser.java:58)
	at com.alibaba.excel.analysis.ExcelAnalyserImpl.choiceExcelExecutor(ExcelAnalyserImpl.java:92)
	at com.alibaba.excel.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文件,完好無損。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM