简体   繁体   中英

Invalid header signature; IOException with Apache POI on excel document

I'm getting:

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

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

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

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

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

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

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.

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.

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.

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

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

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

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

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM