Java POI 提供的数据似乎在 Office 2007+ XML 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31844308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:40:38  来源:igfitidea点击:

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

javaexcelmavenapache-poixssf

提问by Marco Dinatsoli

I am getting this error:

我收到此错误:

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)

org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎在 Office 2007+ XML 中。您正在调用处理 OLE2 Office 文档的 POI 部分。您需要调用 POI 的不同部分来处理此数据(例如 XSSF 而不是 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?

我阅读了 throw Google,我发现我需要使用 XSSF 而不是 HSSF,因为我的 Excel 文件是 xlsx,但是正如您在我的 maven 中看到的,我已经在使用 xlsx。请问我哪里错了?

<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.openwhich is:

我有一个编译错误,new oPCPackage.open其中是:

OPCPackage.open cannot be resolved to a type

OPCPackage.open 无法解析为类型

采纳答案by rgettman

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

根据Apache POI 快速指南POIFSFileSystem(或类似地,NPOIFSFileSystem)仅用于 .xls(Excel 版本到 2003 年)文档。

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

.xlsx 文档 (Excel 2007+) 的等效文件是OPCPackage.

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

You can create an XSSFWorkbookfrom the OPCPackage:

您可以XSSFWorkbook从以下位置创建一个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 Fileinstead of an InputStream, to save memory.

通常最好使用 aFile而不是 ,创建工作簿InputStream以节省内存。

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

此外,如果您想要不关心它是 .xls 还是 .xlsx 的代码:

// or "file.xlsx"
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));

回答by cs_pupil

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.

我将 XSSF 与 xlsx 文件一起使用,但是当我尝试处理使用密码加密/保护的文件时出现此错误。

Once I removed the password, everything worked as expected.

删除密码后,一切都按预期进行。