Why do I failed to read Excel 2007 using POI?

后端 未结 1 885
囚心锁ツ
囚心锁ツ 2020-12-18 15:25

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          


        
相关标签:
1条回答
  • 2020-12-18 16:25

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

    Edit:

    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))

    0 讨论(0)
提交回复
热议问题