java 使用 POI 的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10142282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:45:58  来源:igfitidea点击:

Exception using POI

javaapache-poi

提问by jptiancai

I'm using poi 3.7 , upload the file is .xlsx

我正在使用 poi 3.7 ,上传文件是 .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

回答by Sergei

Just use org.apache.poi.ss.usermodel.WorkbookFactoryinstead of creating instances : new HSSFWorkbook() or new XSSFWorkbook().

只需使用 org.apache.poi.ss.usermodel.WorkbookFactory而不是创建实例:new HSSFWorkbook() 或 new XSSFWorkbook()。

Workbook exWorkBook = WorkbookFactory.create(excelInputStream);

回答by codester

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.

不是您可能想听到的答案。但是我发现当密码错误时我会收到此错误。检查对 Decryptor.verifyPassword() 的调用是否在您的代码中返回 true。如果是这样密码应该没问题。

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.

对我来说,我得到了一个错误,代码被忽略并尝试读取文件。然后我得到了“包应该包含一个内容类型部分 [M1.13]”错误。一旦我输入了正确的密码,我就会返回 true 并且文件被解密。

Hope this helps

希望这可以帮助

回答by Howard Schutzman

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:

我使用 POI,有时当发生这种情况时,您只需要尝试找出问题所在。以下是我过去所做的一些事情,以帮助找出问题所在:

  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.

  1. 将文件转换为 .xls 格式并查看它是否加载。如果是,请重新另存为 .xlsx 并重试。

  2. 如果文件有多个工作表,请尝试将每个工作表另存为一个单独的文件,看看它们是否可以加载,

  3. 如果您将范围缩小到特定的工作表,请加载工作表的部分内容并查看哪个部分导致了问题。

Usually, if you use this "divide and conquer" approach, you can figure out the problem pretty quickly.

通常,如果您使用这种“分而治之”的方法,您可以很快找出问题所在。

Formulas and macros can be particularly problematic.

公式和宏可能特别成问题。

回答by Tarang joshi

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.

如果您使用开放式办公室并尝试以 xlsx 格式保存文件,您仍然会收到错误消息;同样使用 xssf 并不能解决问题。您需要使用 Microsoft Office Excel 工作表来避免错误。

回答by Hyman

I found that I had corrupted my xlsx file and so had to delete it and recreate it.

我发现我损坏了我的 xlsx 文件,所以不得不删除它并重新创建它。