1. POI
POI来自Apache基金会,是其顶级域名项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
2. Excel 2003和2007之区别
微软针对Excel在2007版本之后的实现中使用了ooxml的格式来描述内容信息,和之前2003版本的excel在实现上有所作为,故在POI中提供了一个新的类来兼容2007版的excel的读取和创建过程。
1 HSSF - 提供读写Microsoft Excel格式档案的功能。 2 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HSSF,XSSF的功能列表对比
3.问题描述
在上传excel文件中,解析excel文件,并解析中出现了如下错误信息:
1 Apr 13, 2015 1:29:08 PM org.apache.catalina.core.StandardWrapperValve invoke 2 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 3 org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] 4 at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:203) 5 at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:673) 6 at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274) 7 at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) 8 at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:258) 9 at com.creditease.bsettle.basic.controller.BasicDataController.uploadBusinessDept(BasicDataController.java:350) 10 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 11 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 12 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 13 at java.lang.reflect.Method.invoke(Method.java:597) 14 at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) 15 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 16 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 17 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) 18 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:685) 19 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 20 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919) 21 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851) 22 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) 23 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855) 24 at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 25 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) 26 at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 27 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 28 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 29 at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) 30 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) 31 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 32 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 33 at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180) 34 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) 35 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 36 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 37 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 38 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106) 39 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 40 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 41 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 42 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 43 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 44 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 45 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 46 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 47 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 48 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 49 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 50 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 51 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 52 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 53 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 54 at java.lang.Thread.run(Thread.java:662)
在原有的代码中,解析excel的内容如下:
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(file.getInputStream());
4.解决方案
为了解决针对excel 2003和excel 2007的多种格式,使用如下代码,提供了良好的兼容性:
1 Workbook workbook = WorkbookFactory.create(file.getInputStream()); 2 Sheet hssfSheet = workbook.getSheetAt(0);
5.总结
对Excel的读取,主要涉及工作薄,工作薄,行数据,单元格等的处理,POI对97-2003和2007+两个版本的处理采用不同的类,如下图所示:
其中:
a)工作簿,表格,行,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