Exception using POI

吃可爱长大的小学妹 提交于 2019-12-18 05:22:34

问题


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:

  1. Convert the file to .xls format and see if it loads. If it does, resave as .xlsx and try again.

  2. If the file has multiple sheets, try saving each sheet as a separate file and see if they can load,

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!