I have an excel (.xls) file - Export.xls.
When I manually try to open it, I get the following message:
"The file format and extension of 'Export.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"
If I click on 'Yes' and proceed, the file opens. Then, if I 'Save As' Excel 97-2003 Workbook (*.xls), the file is saved and the next time I open it I don't get the above error message.
Now, my need is to do this via Java code. Because due to restrictions, I cannot fix it manually before I run the Java program.
My working code for a non-corrupted (regular) file below:
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(expFile));
String expFileText = new ExcelExtractor(expWorkBook).getText();
System.out.println("Export xls file text: " + expFileText);
This works fine on a regular xls file. However, for the above file (if I haven't manually saved it with a different format), the Java program throws the below error:
Invalid header signature; read 0x6D78206C6D74683C, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
How can I fix this?
That seems plain text "mx lm...".
Often CSV text files are given an .xls extension so the browser opens the file with Excel.
Rename the file as .txt and check the charset encoding, and read it with the correct charset. Maybe a CSV reader.