且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Apache POI 读取 xlsx 文件时出现异常(org.apache.poi.openxml4j.exception - 无内容类型 [M1.13])?

更新时间:2022-03-14 16:58:36

错误告诉您 POI 找不到 OOXML 文件的核心部分,在本例中是内容类型部分.您的文件不是有效的 OOXML 文件,更不用说有效的 .xlsx 文件了.不过它是一个有效的 zip 文件,否则你会得到一个较早的错误

The error is telling you that POI couldn't find a core part of the OOXML file, in this case the content types part. Your file isn't a valid OOXML file, let alone a valid .xlsx file. It is a valid zip file though, otherwise you'd have got an earlier error

Excel 真的可以加载这个文件吗?我希望它不能,因为异常通常是通过给 POI 一个常规的 .zip 文件来触发的!我怀疑您的文件无效,因此例外

Can Excel really load this file? I'd expect it wouldn't be able to, as the exception is most commonly triggered by giving POI a regular .zip file! I suspect your file isn't valid, hence the exception

.

更新:在 Apache POI 3.15(从 beta 1 开始)中,有一组更有用的异常消息可以解决此问题的更常见原因.在这种情况下,您现在将获得更多描述性异常,例如 ODFNotOfficeXmlFileExceptionOLE2NotOfficeXmlFileException.仅当 POI 确实不知道您提供了什么但知道它已损坏或无效时,才会显示此原始表单.

Update: In Apache POI 3.15 (from beta 1 onwards), there's a more helpful set of Exception messages for the more common causes of this problem. You'll now get more descriptive exceptions in this case, eg ODFNotOfficeXmlFileException and OLE2NotOfficeXmlFileException. This raw form should only ever show up if POI really has no clue what you've given it but knows it's broken or invalid.