• 使用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文件,完好无损。

  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    MySQL-数据库三范式
    去除IDEA中xml黄色背景
    git查看commit提交记录详情
    spring-定时任务<task:scheduled-tasks>
  • 原文地址:https://www.cnblogs.com/remote/p/14441442.html
Copyright © 2020-2023  润新知