问题
I'm using poi 3.7 , upload the file is .xlsx
The console show:
org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:186)
at poi.POITest.ReadAndPrintExcelFile(POITest.java:15)
at poi.POITest.main(POITest.java:59)
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:147)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:592)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:222)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
... 3 more
回答1:
Just use
org.apache.poi.ss.usermodel.WorkbookFactory
instead of creating instances : new HSSFWorkbook() or new XSSFWorkbook().
Workbook exWorkBook = WorkbookFactory.create(excelInputStream);
回答2:
Not the answer you may want to hear. But i found i get this error when the password is wrong. Check if the call to Decryptor.verifyPassword() returns true in your code. If so password should be fine.
For me I got a false, and the code ignored and try to read the file anyway.Then i got the "Package should contain a content type part [M1.13]" error. Once i typed in the correct password, i got true returned and the file was decrypted.
Hope this helps
回答3:
I use POI, and sometimes when this happens you just have to experiment to pinpoint the problem. Here are things I have done in the past to help figure out what the problem is:
Convert the file to .xls format and see if it loads. If it does, resave as .xlsx and try again.
If the file has multiple sheets, try saving each sheet as a separate file and see if they can load,
If you narrow it down to a specific sheet, load parts of the sheet and see which part causes the problem.
Usually, if you use this "divide and conquer" approach, you can figure out the problem pretty quickly.
Formulas and macros can be particularly problematic.
回答4:
If you are using open office and trying to save the file in xlsx format still you get the error; also using xssf does'nt solve the purpose.you need to use Microsoft Office Excel sheet to avoid the error.
回答5:
I found that I had corrupted my xlsx file and so had to delete it and recreate it.
来源:https://stackoverflow.com/questions/10142282/exception-using-poi