Java 无效的标头签名;Excel 文档上的 Apache POI 的 IOException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677925/
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-08-14 03:25:19  来源:igfitidea点击:

Invalid header signature; IOException with Apache POI on excel document

javaapacheexcelapache-poi

提问by Simeon

I'm getting:

我越来越:

java.io.IOException: Invalid header signature; read 0x000201060000FFFE, expected 0xE11AB1A1E011CFD0

java.io.IOException: 无效的头签名;读取 0x000201060000FFFE,预期为 0xE11AB1A1E011CFD0

when trying to add some custom properties to an Excel document using apache POI HPSF.

尝试使用 apache POI HPSF 将一些自定义属性添加到 Excel 文档时。

I'm completely sure the file is Excel OLE2 (not HTML, XML or something else that Excel doesn't complain about).

我完全确定该文件是 Excel OLE2(不是 HTML、XML 或 Excel 不会抱怨的其他内容)。

This is a relevant part of my code:

这是我的代码的相关部分:

try {
     final POIFSFileSystem poifs = new POIFSFileSystem(event.getStream());
     final DirectoryEntry dir = poifs.getRoot();
     final DocumentEntry dsiEntry = (DocumentEntry)
             dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);

     final DocumentInputStream dis = new DocumentInputStream(dsiEntry);
     final PropertySet props = new PropertySet(dis);
     dis.close();
     dsi = new DocumentSummaryInformation(props);
    }
    catch (Exception ex) {
        throw new RuntimeException
            ("Cannot create POI SummaryInformation for event: " + event +
              ", path:" + event.getPath() + 
              ", name:" + event.getPath() +
              ", cause:" + ex);
    }

I get the same error when trying with word and power point files (also OLE2).

尝试使用 word 和 power point 文件(也是 OLE2)时,我遇到了同样的错误。

I'm completely out of ideas so any help/pointers are greatly appreciated :)

我完全没有想法,所以非常感谢任何帮助/指针:)

采纳答案by Gagravarr

If you flip the signature number round, you'll see the bytes of the start of your file:

如果您翻转签名编号,您将看到文件开头的字节:

0x000201060000FFFE -> 0xFE 0xFF 0x00 0x00 0x06 0x01 0x02 00

0x000201060000FFFE -> 0xFE 0xFF 0x00 0x00 0x06 0x01 0x02 00

The first two bytes look like a Unicode BOM, 0xFEFF means 16 bit little endian. You then have some low control bytes, the hex codes for 0 then 258 then 2, so maybe it isn't a text file after all.

前两个字节看起来像 Unicode BOM,0xFEFF 表示 16 位小端。然后你有一些低控制字节,0 然后是 258 然后是 2 的十六进制代码,所以也许它毕竟不是一个文本文件。

That file really isn't an OLE2 file, and POI is right to give you the error. I don't know what it is, but I'm guessing that perhaps it might be part of an OLE2 file without it's outer OLE2 wrapper? If you can open it with office, do a save-as and POI should be fine to open that. As it stands, that header isn't an OLE2 file header so POI can't open it for you.

该文件确实不是 OLE2 文件,POI 正确地向您提供错误。我不知道它是什么,但我猜它可能是 OLE2 文件的一部分,而没有它的外部 OLE2 包装器?如果你可以用office打开它,做一个另存为,POI应该可以打开它。就目前而言,该标头不是 OLE2 文件标头,因此 POI 无法为您打开它。

回答by Fabio

In my case, the file was a CSV file saved with the .xlsextension. Excel was able to open it without a problem, but POI was not.

就我而言,该文件是使用.xls扩展名保存的 CSV 文件。Excel 能够毫无问题地打开它,但 POI 却没有。

If I find a better/more general solution, I'll come back and write it up here.

如果我找到更好/更通用的解决方案,我会回来写在这里。

回答by sridhar

Try save it as csv file directly and use opencsvfor your operations.
Use the following link to know about opencsv.
http://opencsv.sourceforge.net/#what-is-opencsv

尝试直接将其另存为 csv 文件并使用opencsv进行操作。
使用以下链接了解 opencsv。
http://opencsv.sourceforge.net/#what-is-opencsv

Excel can open a csv, xls or even html table saved as xls.

Excel 可以打开保存为 xls 的 csv、xls 甚至 html 表格。

So you can save the file as file_name.csv and can use opencsv for reading the file in your code.

因此,您可以将文件另存为 file_name.csv,并可以使用 opencsv 读取代码中的文件。

Or else you can the file once in excel by save As excel 97-2003 workbook.

否则,您可以通过将文件另存为 excel 97-2003 工作簿在 excel 中保存一次。

And then, POI itself can read the file :-)

然后,POI 本身可以读取文件:-)

回答by user3244162

I had the same problem with an xls file generated by software, I am forced to save files with Excel (the same format) to be able to read with apache POI.

我对软件生成的 xls 文件有同样的问题,我被迫用 Excel(相同格式)保存文件,以便能够用 apache POI 读取。

回答by S.M_Emamian

because you saved your file by Excel 2013. save As your file as excel 97-2003 format.

因为您通过 Excel 2013 保存了您的文件。另存为您的文件为 excel 97-2003 格式。

回答by Amal lal T L

I was using the .xlsx file instead of .xls. We have to use the .xls file if we are using Workbook, Sheet and Row classes. My file was .xlsx, that created this issue and I changed it to .xls, it worked.

我使用的是 .xlsx 文件而不是 .xls。如果我们使用 Workbook、Sheet 和 Row 类,我们必须使用 .xls 文件。我的文件是 .xlsx,它产生了这个问题,我将它更改为 .xls,它起作用了。