Java – Apache POI HSSF XLS reading error

apache-poiexceljava

Using the following code while reading in a .xls file, where s is the file directory:

InputStream input = new FileInputStream(s);
Workbook wbs = new HSSFWorkbook(input);

I get the following error message:

Exception in thread "main" java.io.IOException: Invalid header signature; read 0x0010000000060809, expected 0xE11AB1A1E011CFD0

I need a program that is able to read in either XLSX or XLS, and using the exact same code just adjusted for XSSF it has no problem at all reading in the XLSX file.

Best Answer

If the file is in xlsx format instead of xls you might get this error. I would try using the generic Workbook object (Also called the SS Usermodel)

Check out the Workbook interface and the WorkbookFactory object. The factory should be able to create a generic Workbook for you out of either xlsx or xls.

I thought I had a good tutorial on this, but I can't seem to find it. I'll keep looking though.

Edit

I found this little tiny snippet from Apache's site about reading and rewriting using the SS Usermodel.

I hope this helps!

Related Topic