InvalidFormatException Java中的POI [英] InvalidFormatException in Java POI

查看:2099
本文介绍了InvalidFormatException Java中的POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我收到的异常:

org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

我试图打开该文件是.xls文件,我搜索了可能的解决方案,我发现这个和<一个href=\"http://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma\">this,但我已经这样做了正确的,所以这不是问题。

The file I am trying to open is an .xls file, I searched for possible solutions, I found this and this, but I am already doing this correctly, so this is not the problem.

我的code:

InputStream file = new FileInputStream(new File(path));
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheetNumber);

工作表NUMBER是一个int(总汇入作业0在我的软件)和路径是正确的,我这张贴,以确保我没有失败之前有测试这两个多次。即抛出异常的行是:

sheetNumber is an int (It´s always 0 in my software) and the path is correct, I tested these two many times before posting this, to make sure I am not failing there. The line that is throwing the exception is:

org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);

(如果你想在code更多信息,你可以找到全班的这里

有谁知道我在哪里可以得到失败?谢谢大家。

Does anyone know where can I be failing? Thank you all.

推荐答案

促进一些意见的答案

您第一个问题是您在使用Apache POI的版本。如果你使用POI 3.15 Beta 1版本或更高版本,你会得到在这种情况下更加有用的异常。

Your first issue is the version of Apache POI you're using. If you were to use POI 3.15 beta 1 or newer, you'd get a much more helpful Exception in this situation.

在POI 3.15 Beta 1中,更新的,你会得到一个<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.html\"相对=nofollow> ODFNotOfficeXmlFileException 一条消息,如:

In POI 3.15 beta 1 and newer, you'll get a ODFNotOfficeXmlFileException with a message like:

ODFNotOfficeXmlFileException: The supplied data appears to be in ODF (Open Document) Format. Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit

正因为如此,你可以找到(和已经找到,谢谢!)什么你的文件确实是使用的 Apache的蒂卡CLI应用 - 检测模式。当你发现,这给了应用程序/ vnd.oasis.opendocument.s preadsheet 这是一个开放文档格式S preadsheet,通常与的.ods 扩展。因此,有人更名为的.ods 文件复制到的.xlsx ,这虽然可能使Excel中打开它,没有按' ŧ奇迹般地改变格式!

As it is, you can find (and have found, thanks!) out what your file really is by using the Apache Tika CLI App in --detect mode. As you found, that gave application/vnd.oasis.opendocument.spreadsheet which is an OpenDocument Format Spreadsheet, normally with the .ods extension. So, someone has renamed the .ods file to .xlsx, which while it may make Excel open it, doesn't magically change the format!

的Apache POI不支持ODF格式,所以你要么需要将文件转换为真正的Excel格式,或者使用类似的 Apache的ODF工具包来处理它。

Apache POI doesn't support the ODF formats, so you'll either need to convert the file to a true Excel format, or use something like Apache ODF Toolkit to process it.

这篇关于InvalidFormatException Java中的POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆