• Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决


    引言:在Java中,使用POI来进行excel的读取和解析是常用的一种做法;在办公室的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的读取和创建过程。

    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之后,后台出现了上述错误;针对正常的情况下的excel是没有问题的;经过分析上传文件发现,应该是2007和2003文件格式不同导致的问题。

      在原有的代码中,解析excel的内容如下:

    XSSFWorkbook hssfWorkbook = new XSSFWorkbook(file.getInputStream());
      由此可知,目前的代码中只能支持Excel 2007的格式解析,而不能支持2003格式。故此产生了异常信息。

    4.解决方案

        为了解决针对excel 2003和excel 2007的多种格式,使用如下代码,提供了良好的兼容性: 

    1 Workbook workbook = WorkbookFactory.create(file.getInputStream());
    2 Sheet hssfSheet = workbook.getSheetAt(0);
        这里我们使用了一个抽象类工作簿来访问所有的格式下的excel,虽然某些特性,某些2007下的内容可能无法正常读取,但是主要的内容是可以正常读取的。

    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

  • 相关阅读:
    [Leetcode] Distinct Subsequences
    [Leetcode] Restore IP Addresses
    [Leetcode] Substring with Concatenation of All Words
    [Leetcode] Palindrome Partitioning II
    [Leetcode] Palindrome Partitioning
    [Leetcode] Maximal Rectangle
    [Jobdu] 题目1493:公约数
    [Leetcode] Merge k Sorted Lists
    [Leetcode] Gray Code
    opencv2-新特性及Mat
  • 原文地址:https://www.cnblogs.com/AI-xiaocai/p/11730359.html
Copyright © 2020-2023  润新知