Java – Use POI to parse Excel but got exception “Invalid Header Signature”

apache-poiexceljava

I was trying to use Apache POI (Version 3.6) to parse Excel .xls file, but got only Exception:

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

I have Googled some result, which basically said that "The file is actually not a valid excel file (i.e. .csv and so on) but ended with the suffix .xls". But I'm quite sure that my excel file is valid (in Excel97-2003 format).

For secrecy considerations, I couldn't post my excel, but when I use emacs hexl-mode to view this binary excel file, the header is:

D0CF 11E0 A1B1 1AE1

I think it is just what POI expected (E11AB1A1E011CFD0, but in big-endian). But why I got the exception?

BTW, if I use vim with command %!xxd to view the same excel file, I got a header different from eamcs:

C390 C38F 11C3 A0C2

And the whole binary file seems totally different. I cannot understand.

Thanks for any of your help!

Best Answer

If you get that exception, then your file really isn't a true .xls file. It will instead either be some other file, renamed to have a .xls extension, or a corrupted file.

I'd suggest you try opening the file in Excel, and do a Save-As. That may give you a hint as to the file type. If not, do a save-as as Excel .xls, and then you'll be able to open that file

I don't know what you file is (I don't recognise the header), but I can assure you that it isn't an OLE2 header as a valid .xls file would have.

It's possible that Apache Tika may be able to work out what kind of binary file it is, so you could always try with the Tika-App jar

Related Topic