Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

When I'm reading Excel file(.xls format), I keep getting an Exception :

java.lang.IllegalArgumentException: Your Input Stream was neither an OLE 2 stream, nor an OOXML stream.

I Go-ogled and found that if the input stream is not supporting reset or mark, I should wrap it with pushbackStream. My input stream is not mark\reset supported.

So using pushbackStream is the only option? How to use it? And whats the use of it?

Thanks

like image 655
mee Avatar asked Apr 25 '13 08:04

mee


People also ask

How do you convert InputStream to OutputStream?

Using InputStream. In Java 9 or higher, you can use the InputStream. transferTo() method to copy data from InputStream to OutputStream . This method reads all bytes from this input stream and writes the bytes to the given output stream in the original order.


1 Answers

Your InputStream was neither an OLE2 stream, nor an OOXML stream
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

I guess you are using Workbook Factory OR a different format input file and different workbook type. This error usually pops up when its not able to not able to read the file type. Apache POI does not check the file extension. If you open it in a text editor, you'll see that instead it'll be in a different format. Or you might be initializing the workbook type to HSSF or XSSF, before using Workbook Factory.

Simpler solution is to open the file using Microsoft Excel and save it as another file(using File>Save As option from Microsoft Excel > Menu).

Workbook Factory does not check the file extension, Instead it checks the file MIME type. Basically excel works with different files(Eg: the files created using third party applications, excel 2003 version), but Apache POI is very specific.

PushbackInputStream adds "push back" or "unread" functionality to another input stream. It allows you to read ahead a few bytes to see what is coming, before you can determine how to interpret the current byte.

If you are not using Workbook Factory, PushbackInputStream is the only alternative I guess.

If you can share the code here I can test it and reconfirm it.

like image 170
Tejus Prasad Avatar answered Sep 30 '22 14:09

Tejus Prasad