Java POI read Excel package should contain a content type part [M1.13]] with root cause exception problem resolution

Source: Internet
Author: User

Introduction: In Java, the use of POI to read and parse Excel is a common practice; there are 2007,2003 two different formats in Office Excel, often differentiated by different suffixes of xls/xlsx, but users may not know the difference, Therefore, there will be an incompatible format exception

1. POI

Poi originates from the Apache Foundation, is its top-level domain project, and POI provides APIs to Java programs to read and write to Microsoft Office format archives.

2. Differences between Excel 2003 and 2007

Microsoft has used the Ooxml format to describe content information for Excel in its implementation after version 2007, and the previous 2003 version of Excel has been implemented differently, so a new class is available in the POI that is compatible with the 2007 version of Excel's read and create process.

HSSF-provides the ability to read and write files in Microsoft Excel format. XSSF-provides the ability to read and write Microsoft Excel ooxml format files.

3. Description of the problem

In the upload Excel file, parse the Excel file, and the following error message appears in parsing:

APR 1:29:08 PM org.apache.catalina.core.StandardWrapperValve invokeSEVERE:Servlet.service () for Servlet [ Appservlet] In the context with the 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 causeorg.apache.poi.openxml4j.exceptions.InvalidFormatException:Package should contain a Content type Part [M1.13]at Org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl ( at Org.apache.poi.openxml4j.opc.OPCPackage.getParts ( at ( at ( at org.apache.poi.xssf.usermodel.xssfworkbook.<init> ( at Com.creditease.bsettle.basic.controller.BasicDataController.uploadBusinessDept ( at Sun.reflect.NativeMethodAccessorIMPL.INVOKE0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( at Sun.reflect.DelegatingMethodAccessorImpl.invoke (DELEGATINGMETHODACCESSORIMPL.JAVA:25) at Java.lang.reflect.Method.invoke ( at ( at ( ) at Org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle ( SERVLETINVOCABLEHANDLERMETHOD.JAVA:104) at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod ( at Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal ( at Org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (Abstracthandlermethodadapter.jaVA:80) at Org.springframework.web.servlet.DispatcherServlet.doDispatch ( at Org.springframework.web.servlet.DispatcherServlet.doService ( at Org.springframework.web.servlet.FrameworkServlet.processRequest ( at Org.springframework.web.servlet.FrameworkServlet.doPost ( at Javax.servlet.http.HttpServlet.service ( at Org.springframework.web.servlet.FrameworkServlet.service ( at Javax.servlet.http.HttpServlet.service ( at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( at Org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal ( at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( at Org.apache.catalina.core.ApplIcationfilterchain.internaldofilter ( at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( at ( at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( at Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal ( at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( at Org.apache.catalina.core.StandardWrappervalve.invoke ( at Org.apache.catalina.core.StandardContextValve.invoke ( at Org.apache.catalina.authenticator.AuthenticatorBase.invoke ( at Org.apache.catalina.core.StandardHostValve.invoke ( at Org.apache.catalina.valves.ErrorReportValve.invoke ( at Org.apache.catalina.valves.AccessLogValve.invoke ( at Org.apache.catalina.core.StandardEngineValve.invoke ( at Org.apache.catalina.connector.CoyoteAdapter.service ( at Org.apache.coyote.http11.AbstractHttp11Processor.process ( at Org.apache.coyote.abstractprotocol$abstractconnectionhandler.process ( at$ ( at Java.util.concurrent.threadpoolexecutor$worker.runtask ( at Java.util.conCurrent. Threadpoolexecutor$ ( at (
In the process of user operation, after uploading Excel, the above error occurred in the background, for the normal situation of Excel is not a problem, after analysis of the upload file found, should be 2007 and 2003 file format caused by different problems.

In the original code, the contents of the parsing Excel are as follows:

Xssfworkbook Hssfworkbook = new Xssfworkbook (File.getinputstream ());
It can be concluded that the current code only supports format resolution for Excel 2007 and cannot support 2003 format. Therefore, abnormal information is generated.

4. Solution

To resolve multiple formats for Excel 2003 and Excel 2007, use the following code to provide good compatibility:

  Workbook Workbook = Workbookfactory.create (File.getinputstream ());  Sheet hssfsheet = workbook.getsheetat (0);  Schematic access to sheet
Here we use an abstract class workbook to access all the formats of Excel, although some features, such as 2007, may not read properly, but the main content is readable.

5. Summary

The reading of Excel mainly involves the processing of workbooks, workbooks, rows of data, cells, etc., and the POI handles 97-2003 and 2007+ two versions with different classes, as shown in:

A) Workbook, Sheet, Row, cell and so on as the interface;
b) Hssfworkbook, Hssfsheet, Hssfrow, Hssfcell for the 97-2003 version of the corresponding processing implementation class;
c) Xssfworkbook, Xssfsheet, Xssfrow, Xssfcell for the 2007+ version of the corresponding processing implementation class;


1. Read Excel Files (compatible with 97-2003 and 2007+ two formats) via POI

2. Org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma

Java POI read Excel package should contain a content type part [M1.13]] with root cause exception problem resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.