Home » excel » java – Why do I failed to read Excel 2007 using POI?

java – Why do I failed to read Excel 2007 using POI?

Posted by: admin May 14, 2020 Leave a comment


When I try to initialize a Workbook object I always get this error:

The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

But I followed the office sample to do this, following is my code:

File inputFile = new File(inputFileName); 
InputStream is = new FileInputStream(inputFile);
Workbook wb = new XSSFWorkbook(is);

Exception occurs at code line:

Workbook wb = new XSSFWorkbook(is);

Here is POI jar including:


Can any guys give me guidance? An example showing how to read a complete Excel 2007 document will be appreciated. Thanks in advance!

How to&Answers:

I assume that you have recheck that your original file is indeed in Office 2007+XML format, right?


Then, if you are sure the format is ok, and it works for you using the WorkbookFactory.create, you can find the answer in the code of such method:

     * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
     *  the given InputStream.
     * Your input stream MUST either support mark/reset, or
     *  be wrapped as a {@link PushbackInputStream}!
    public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
            // If clearly doesn't do mark/reset, wrap up
            if(! inp.markSupported()) {
                    inp = new PushbackInputStream(inp, 8);

            if(POIFSFileSystem.hasPOIFSHeader(inp)) {
                    return new HSSFWorkbook(inp);
            if(POIXMLDocument.hasOOXMLHeader(inp)) {
                    return new XSSFWorkbook(OPCPackage.open(inp));
            throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");

This is the bit that you were missing: new XSSFWorkbook(OPCPackage.open(inp))