Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决
引言: 在Java中,使用POI来进行excel的读取和解析是常用的一种做法;在office的excel中存在2007,2003两种不同的格式,通常情况下是以xls/xlsx的不同后缀来区分的,但用户可能不知道这个区别,所以会出现格式无法兼容的异常
1. POI
POI源自Apache基金会,是其顶级域名项目, POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
2. Excel 2003和2007之区别
微软针对Excel在2007版本之后的实现中使用了ooxml的格式来描述内容信息,和之前2003版本的excel在实现上有所区别,故在POI中提供了一个新的类来兼容2007版的excel的读取和创建过程。
- HSSF - 提供读写Microsoft Excel格式档案的功能。
- XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HSSF,XSSF的功能列表对比
3. 问题描述
在上传excel文件中,解析excel文件,并解析中出现了如下错误信息:
- Apr 13, 2015 1:29:08 PM org.apache.catalina.core.StandardWrapperValve invoke
- SEVERE: Servlet.service() for servlet [appServlet] in context with path [/bsettle] threw exception [Request processing failed; nested exception is org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]] with root cause
- 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:203)
- at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:673)
- at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)
- at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
- at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:258)
- at com.creditease.bsettle.basic.controller.BasicDataController.uploadBusinessDept(BasicDataController.java:350)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
- at java.lang.reflect.Method.invoke(Method.java:597)
- at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
- at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
- at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685)
- at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
- at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
- at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
- at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:662)
在用户操作过程中,上传excel之后,后台出现了上述错误;针对正常的情况下的excel是没有问题的;经过分析上传文件发现,应该是2007和2003文件格式不同导致的问题。
在原有的代码中, 解析excel的内容如下:
- XSSFWorkbook hssfWorkbook = new XSSFWorkbook(file.getInputStream());
由此可知,目前的代码中只能支持Excel 2007的格式解析,而不能支持2003格式。 故此产生了异常信息.
4. 解决方案
为了解决针对excel 2003 和 excel 2007 的多种格式,使用如下代码,提供了良好的兼容性:
- Workbook workbook = WorkbookFactory.create(file.getInputStream());
- Sheet hssfSheet = workbook.getSheetAt(0); //示意访问sheet
这里我们使用了一个抽象类Workbook来访问所有的格式下的excel,虽然某些特性,比如2007下的内容可能无法正常读取,但是主要的内容是可以正常读取的。
5. 总结
对Excel的读取,主要涉及工作薄、工作薄、行数据、单元格等的处理,POI对97-2003和2007+两个版本的处理采用不同的类,如下图所示:
其中:
a)Workbook、Sheet、Row、Cell等为接口; b)HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell为97-2003版本对应的处理实现类; c)XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell为2007+版本对应的处理实现类;参考资料:
1. http://cgs1999.iteye.com/blog/1525665 通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)
2. http://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma