相关文章推荐
想发财的花卷  ·  Docker : exec ...·  3 月前    · 
奋斗的炒饭  ·  Out-File ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

org.apache.poi.poifs.filesystem.OfficeXmlFileException: 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 (e.g. XSSF instead of HSSF)

I read throw Google and I found out that I need to use XSSF instead of HSSF because my Excel file is xlsx, but as you see in my maven, I am already using xlsx. Where have I gone wrong please?

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.13-beta1</version>
    </dependency> 

The code the makes the exception is:

POIFSFileSystem fs;
            fs = new POIFSFileSystem(new FileInputStream(getFilePath()));

My new code

public void getBColum() {
    try {
        OPCPackage fs;
        fs = new OPCPackage.open(new File(getFilePath()));
        XSSFWorkbook wb = new XSSFWorkbook(fs);
        XSSFSheet sheet = wb.getSheet("Master column name - Used Car");
        XSSFRow row;
        CellReference cr = new CellReference("A1");
        row = sheet.getRow(cr.getCol());
        System.out.println(row.getCell(3));
    } catch (FileNotFoundException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("How can this error be possible? we should have already thrown an exception in the construction");
    } catch (IOException e) {
        logger.error(String.format("Exception in reading the file: %s",
                e.getMessage()));

I have a compile error in new oPCPackage.open which is:

OPCPackage.open cannot be resolved to a type

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"));
                there is wrong in the first line opcpackage.open  .... OPCPackage.open cannot be resolved to a type
– Marco Dinatsoli
                Aug 5, 2015 at 23:36
                Now that I see your code, remove the "new" -- OPCPackage.open is a static method.  Try fs = OPCPackage.open(new File(getFilePath()));.
– rgettman
                Aug 5, 2015 at 23: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.

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;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.