Java POI the supplied data appears to be in the Office 2007+ XML

后端 未结 3 2011
陌清茗
陌清茗 2020-12-03 07:22

I am getting this error:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are call

相关标签:
3条回答
  • 2020-12-03 07:50

    I was using XSSF with a xlsx file, but got this error when I tried to process a file that was encrypted/protected with a password.

    Once I removed the password, everything worked as expected.

    0 讨论(0)
  • 2020-12-03 07:52

    According to the Apache POI Quick Guide, the POIFSFileSystem (or similarly, NPOIFSFileSystem) is only used with .xls (Excel versions through 2003) documents.

    The equivalent for .xlsx documents (Excel 2007+) is OPCPackage.

    OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
    

    You can create an XSSFWorkbook from the OPCPackage:

    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    

    Or you can just create it directly:

    XSSFWorkbook wb = new XSSFWorkbook(new File("file.xlsx"));
    

    Generally it's better to create the workbook using a File instead of an InputStream, to save memory.

    Also, if you want code that doesn't care whether it's an .xls or an .xlsx:

    // or "file.xlsx"
    Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
    
    0 讨论(0)
  • 2020-12-03 08:01

    well actually there is no OPCPackage, I am using https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml/3.5-beta5 so you have to:

    import org.apache.poi.openxml4j.opc.Package;
    ....
    Package fs = Package.open(new ByteArrayInputStream(container.getContent()));
                XSSFWorkbook wb = new XSSFWorkbook(fs);
                XSSFSheet sheet = wb.getSheetAt(0);
                XSSFRow row;
                XSSFCell cell;
    
    0 讨论(0)
提交回复
热议问题