无效的头签名;在excel文档上使用Apache POI

时间:2022-05-06 20:24:06

I'm getting:

我得到:

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

. io .IOException:无效的头签名;读0 x000201060000fffe xe11ab1a1e011cfd0预期0

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 :)

我完全没有主意了,所以非常感谢你的帮助和建议。

5 个解决方案

#1


5  

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 0x00 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位的endian。然后有一些低控制字节,十六进制编码为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打开它,做一个as和POI应该可以打开它。正如它所显示的,那个头不是一个OLE2文件头,所以POI不能为你打开它。

#2


5  

In my case, the file was a CSV file saved with the .xls extension. 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.

如果我找到一个更好/更一般的解,我会回来写在这里。

#3


1  

Try save it as csv file directly and use opencsv for 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可以打开csv、xls甚至html表保存为xls。

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

因此可以将文件保存为file_name。可以使用opencsv读取代码中的文件。

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

或者你也可以将文件保存为excel 97-2003工作簿。

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

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

#4


0  

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进行读取。

#5


0  

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

因为你用Excel 2013保存了你的文件。将文件保存为excel 97-2003格式。

#1


5  

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 0x00 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位的endian。然后有一些低控制字节,十六进制编码为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打开它,做一个as和POI应该可以打开它。正如它所显示的,那个头不是一个OLE2文件头,所以POI不能为你打开它。

#2


5  

In my case, the file was a CSV file saved with the .xls extension. 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.

如果我找到一个更好/更一般的解,我会回来写在这里。

#3


1  

Try save it as csv file directly and use opencsv for 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可以打开csv、xls甚至html表保存为xls。

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

因此可以将文件保存为file_name。可以使用opencsv读取代码中的文件。

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

或者你也可以将文件保存为excel 97-2003工作簿。

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

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

#4


0  

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进行读取。

#5


0  

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

因为你用Excel 2013保存了你的文件。将文件保存为excel 97-2003格式。