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.
The Exception you're getting is one telling you that the file you're supplying isn't a valid Excel binary file, at least not a valid Excel file produced since about 1990. The exception you're getting tells you what POI expects, and that it found something else instead which wasn't a valid .xls file, and wasn't anything else POI can detect.
One thing to be aware of is that Excel opens a wide variety of different file formats, including .csv and .html. It's also not very picky about the file extension, so will happily open a CSV file that has been renamed to a .xls one. However, since renaming a .csv to a .xls doesn't magically change the format, POI still can't open it!
.
From the exception, I can tell what's happening, and I can also tell you're using an ancient version of Apache POI! A header signature of 0x0010000000060809 corresponds to the Excel 4 file format, from about 25 years ago! If you use a more recent version of Apache POI, it'll give you a helpful error message telling you that the file supplied is an old and largely unsupported Excel file. New versions of POI do include the OldExcelExtractor tool which can pull out some information from those ancient formats.
Otherwise, as with all exceptions of this type, try opening the file in Excel and doing a save-as. That will give you an idea of what the file currently is (eg .html saved as .xls, .csv saved as .xls etc), and will also let you re-save it as a proper .xls file for POI to load and work with.
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!
Invalid header signature; read 0x342E312D46445025, expected 0xE11AB1A1E011CFD0
Well I got this error when I uploaded corrupted xls/xlsx file(to upload corrupt file I renamed sample.pdf to sample.xls). Add validation like :
Workbook wbs = null;
try {
InputStream input = new FileInputStream(s);
wbs = new HSSFWorkbook(input);
} catch(IOException e) {
// log "file is corrupted", show error message to user
}
Related
I am facing an issue when using apache poi to extract an embedded .xlsx files from a .ppt file. It would be really great if somebody could help me out.
The subject of the problem:
Problem trying to solve: Extracting a ".xlsx" file embedded inside a ".ppt".
I am currently using apache-poi.
It seems that when I try to do it using hslfSlideShow.getEmbeddedObjects(), I get the xlsx object just fine but when I try converting it to the XLSFWorkbook object using say WorkbookFactory.create(inputStream), it threw an error saying
java.lang.IllegalArgumentException: The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. Is it really an excel file? Had: [OlePres000, Ole, CompObj, Package]
at org.apache.poi.hssf.usermodel.HSSFWorkbook.getWorkbookDirEntryName(HSSFWorkbook.java:286)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:326)
at org.apache.poi.hssf.usermodel.HSSFWorkbookFactory.createWorkbook(HSSFWorkbookFactory.java:64)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:167)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:112)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:253)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:221)
Interestingly it is calling HSSFWorkbookFactory even though its an xlsx file.
And no the xlsx file is not corrupted/password-protected. I can open it just fine.
Also, it works fine if I try parsing the .xlsx file without embedding it in the .ppt.
And the parsing works fine when I embed it in a .pptx file and call methods such as xmlSlideShow.getAllEmbeddedParts() to get the embedded objects from .pptx.
Promoting some comments and investigation to an answer...
This was a limitation in older version of Apache POI, but was fixed in July in r1880164.
For backwards-compatibility reasons, PowerPoint will often (but not always...) write embedded OOXML resources wrapped in an intermediate OLE2 layer. This has the advantage that tools/programs which expect embedded office documents to be something like a xls / doc to cope, but at the expense of another layer of wrapping.
Newer versions of Apache POI (5.0 should be the first released one with the fix in) have support in WorkbookFactory for receiving an OLE2 wrapper like this, pulling out the underlying xlsx stream and handing that off to XSSFWorkbook. (Older versions did this for OLE2-based password-protected xlsx files, but not their unencrypted cousins)
For now, if you're stuck on an affected POI version, the code you'll want is something like this (largely taken from the unit test verifying support!):
POIFSFileSystem fs = new POIFSFileSystem(data.getInputStream());
if(fs.getRoot().hasEntry("Package")) {
DocumentInputStream dis = new DocumentInputStream((DocumentEntry)fs.getRoot().getEntry("Package"));
try (OPCPackage pkg = OPCPackage.open(dis)) {
XSSFWorkbook wb = new XSSFWorkbook(pkg);
handleWorkbook(wb);
wb.close();
}
} else {
try (HSSFWorkbook wb = new HSSFWorkbook(fs)) {
handleWorkbook(wb);
}
}
I have the issue that Apache POI "corrupted" a xlsm / xlsx file by just reading and writing it (e.g. with the following code)
public class Snippet {
public static void main(String[] args) throws Exception {
String str1 = "c:/tmp/spreadsheet.xlsm";
String str2 = "c:/tmp/spreadsheet_poi.xlsm";
// open file
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(str1)));
// save file
FileOutputStream out = new FileOutputStream(str2);
wb.write(out);
wb.close();
out.close();
}
}
Once you open the spreadsheet_poi.xlsm in Excel you'll get an error like the following
"We found a problem with some content in xxx. Do you want us to try to recover as much as we can..."?
If you say yes you'll end up with a log which could look like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error145040_01.xml</logFileName>
<summary>Errors were detected in file 'C:\tmp\spreadsheet_poi.xlsm'</summary>
<repairedParts>
<repairedPart>Repaired Part: /xl/worksheets/sheet4.xml part with XML error. Load error. Line 2, column 0.</repairedPart>
<repairedPart>Repaired Part: /xl/worksheets/sheet5.xml part with XML error. Load error. Line 2, column 0.</repairedPart>
<repairedPart>Repaired Part: /xl/worksheets/sheet8.xml part with XML error. Load error. Line 2, column 0.</repairedPart>
</repairedParts>
</recoveryLog>
Whats the best approach to debug the issue in more detail (e.g. find out what makes poi to "corrupt" the file?
Eventually I found how that the best approach for debugging this are two things
open the affected workbook (e.g. with 7zip and format the affected sheets with an xml editor (e.g. Notepad++ > Plugins > XML Tools > Pretty print (XML only - with line breaks). After saving the files and updating the xlsm file you'll get the "real" line numbers in the Excel error log. Alternative option (which I haven't tried but should work according to the POI mailing liste: use OOXMLPrettyPrint (https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/ooxml/dev/) to format the file and then reopen it it in excel.
if the real line numbers not already help compare the sheet xml files of the original xlsx file and the one saved by poi. You'll notice that there are differences in regards to the attributes and also the order is different. In order to properly compare I used Beyond Compare with "Additional File Formats" (see https://weblogs.asp.net/lorenh/comparing-xml-files-with-beyond-compare-3-brilliant for more information). Maybe there is another diff tool that is equally good.
In my case the problem was that poi somehow changed the dimension setting from
<dimension ref="A1:XFD147"/>
to
<dimension ref="A1:XFE147"/>
(with XFE beeing a non existing column). I fixed it by removing those many empty columns in the original xlsx file.
My professor said: "How does the mathematician find the lion in the desert?" - "First cuts the desert into two halves, finds out where is the lion, then repeats it until the lion is caught".
So, try to remove features from the Excel files, try different versions, until you find the root cause. There may be multiple causes, though.
I want to read an Excel file (xlsx) that has 19455 row. I am using last version of POI. When my app comes to ;
FileInputStreaminputStream = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(inputStream);
these rows getting PartAlreadyExistException. Also writing the error console;
A part with the name '/xl/sharedStrings.xml' already exists : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]
Why I can't read this excel file?
Check whether the excel corrupted or not...
I was getting same Exception, when i debugged I found ou
i am trying to get cell data from my .csv file but it gets error :
jxl.read.biff.BiffException: Unable to recognize OLE stream
I don't understand how to solve this,please give me some solution
this code is for jxl api & is that api support to .csv?
Code for reference:
public void read() throws IOException, BiffException {
File inputWorkbook = new File(inputFile);
try
{
w = Workbook.getWorkbook(inputWorkbook.getAbsoluteFile());
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (row = 1; row < sheet.getRows(); row++)
{
ReadExcelLotSizeEntity readExcelLotSizeEntity =new ReadExcelLotSizeEntity();
cell = sheet.getCell(1,row);
type= cell.getType();
if (cell.getType() == CellType.LABEL)
{
symbol=cell.getContents();
System.out.println(":::::::::::::::::"+symbol);
readExcelLotSizeEntity.setSymbol(symbol);
}
int col=2;
cell = sheet.getCell(col,row);
while(!cell.getContents().equals("")||cell.getContents()!=null)
{
System.out.println("||||||||||||||||"+cell.getContents());
cell=sheet.getCell(col,row);
col++;
}
lotSize= new Double(cell.getContents());
readExcelLotSizeEntity.setLotSize(lotSize);
readExcelLotSizeEntity.setCreateUserId(1L);
readExcelLotSizeEntity.setCreateDtTm(new Date());
readExcelLotSizeHome.persist(readExcelLotSizeEntity);
}
} catch (BiffException e) {
e.printStackTrace();
}
}
I was also facing this problem earlier. I googled and read this post and many other posts that were asking for solution to this BiffException. I don't have the exact solution but as I fixed my problem you can do it too, perhaps.
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7.
I hope this will work in your case.
Saving File as "Excel 97-2003 Workbook" type solved my issue.
JXL library doesnot support .csv and .xslx formats, which is the format used by Excel-2010. hence, use Excel 97-2003 which is .xls foramatted and is supported by JXL library.
or else if you want to use excel-2010, use APACHE POI(XSSFWorkbooks) instead of JXL.
For using .csv format, google for CSVReader libraries.
JXL is a simple (and hence limited) API. If it says
Unable to recognize OLE stream
it is what it is. It doesn't quite understand your Excel XLS file. Have confidence that the error is legitimate. This API only supports *.xls files; it doesn't support, for example, *.csv or *.xlsx files. Obviously, having the file renamed to *.xls alone is not sufficient. It must be in Excel 97-2003 format too.
Copy all the cells from your *.csv or *.xlsx file.
Open MS Excel and paste the copied cells.
Save the file as MS Excel 97-2003 (*.xls) file.
This error will surely not appear again.
On the other hand, if you want to process other formats (xlsx, csv) directly, look for other tools like Apache POI.
Save the Excel file type as Excel 97-2003 Worksheet and extension type as xls
Actually you are using different version of csv file .Please save it in the exact version.
For ex: we should save the excel sheet in word as 9
save the file as Excel 97-2003 and also change the file format from xlsx to xlx , in the code(in the file name)
I was trying to read data from the Excel file saved in MS Office 2010 and I was getting this error. I saved the file as an Excel 2003-7 and then read it without any problem. It may the case that this problem occurs in Office 10 but not in Office 2003-7
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 :-)
because you saved your file by Excel 2013. save As your file as excel 97-2003 format.
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.
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.