Apache Poi 3.13无法找到打开XLSX文件的类

时间:2020-12-07 20:20:11

I am using apache POI to read and write Excels' files with Java, but I am not able to find WorkbookFactory nor XSSFWorkbook in the sources to read xlsx files.

我正在使用apache POI用Java读取和写入Excels的文件,但是我无法在源代码中找到WorkbookFactory或XSSFWorkbook来读取xlsx文件。

pom.xml :

pom.xml:

<poi.version>3.13</poi.version>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${poi.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>${poi.version}</version>
</dependency>

I can't find neither any information in the changelog of apache poi that could lead to this behaviour.

我在apache poi的更改日志中找不到任何可能导致此行为的信息。

Edit: Here my implementation (just a simple method for the moment)

编辑:这里是我的实现(暂时只是一个简单的方法)

public static HSSFSheet getXLSSheet(String fileName, int sheetIndex) throws IOException {
    InputStream inputStream = new FileInputStream(fileName);
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
    return workbook.getSheetAt(sheetIndex);
}

I tried to open an XLSX file but since I can't find the two other classes (WorkbookFactory or XSSFWorkbook) I was expected to have an error like this one:

我试图打开一个XLSX文件,但由于我找不到另外两个类(WorkbookFactory或XSSFWorkbook),我应该会遇到类似这样的错误:

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 (eg XSSF instead of HSSF)

Thanks in advance.

提前致谢。

2 个解决方案

#1


3  

First up, I can assure you that the WorkbookFactory and XSSFWorkbook classes are contained in the POI-OOXML 3.13 jars, as documented on the POI site

首先,我可以向您保证,WorkbookFactory和XSSFWorkbook类包含在POI-OOXML 3.13罐中,如POI站点上所述

$ unzip -l .m2/repository/org/apache/poi/poi-ooxml/3.13/poi-ooxml-3.13.jar | grep WorkbookFactory
  6041  2015-09-22 00:22   org/apache/poi/ss/usermodel/WorkbookFactory.class

However, as the exception you have posted makes clear, your code will never work for XLSX files, it needs changing. Well, for that and some other issues too... eg Don't use an InputStream if you have a File

但是,正如您发布的例外情况所表明的那样,您的代码永远不会适用于XLSX文件,需要更改。那么,对于那个以及其他一些问题......例如,如果你有一个文件,请不要使用InputStream

Your code should instead be more like:

你的代码应该更像是:

import java.io.File;
import org.apache.poi.ss.usermodel.*;

public static Sheet getExcelSheet(String fileName, int sheetIndex) throws IOException {
   File file = new File(fileName);
   Workbook workbook = WorkbookFactory.create(file);
   return workbook.getSheetAt(sheetIndex);
}

That will work for both xls and xlsx files, and will be lower memory than using an input stream. As long as you tell maven to depend on the poi-ooxml jar, you'll get all the other dependencies you need automatically

这对xls和xlsx文件都有效,并且比使用输入流的内存更低。只要您告诉maven依赖于poi-ooxml jar,您将获得所需的所有其他依赖项

#2


0  

The XSSFWorkbook presents in 3.11 version of Apache POI.

XSSFWorkbook提供了3.11版本的Apache POI。

Source code of apache POI

apache POI的源代码

Try to use this version of apache poi.

尝试使用这个版本的apache poi。

Also, it still present in trunk:

此外,它仍然存在于主干:

Source code of apache POI

apache POI的源代码

I believe the issue is with you maven local storage. try to remove poi folederes from maven local repository and redownload dependencies.

我相信问题在于maven本地存储。尝试从maven本地存储库中删除poi folederes并重新下载依赖项。

Also try to not forget to change all call from HSSFWorkbook to XSSFWorkbook, because XLSX files opens via XSSFWorkbook only.

另外,请不要忘记将所有来自HSSFWorkbook的呼叫更改为XSSFWorkbook,因为XLSX文件仅通过XSSFWorkbook打开。

#1


3  

First up, I can assure you that the WorkbookFactory and XSSFWorkbook classes are contained in the POI-OOXML 3.13 jars, as documented on the POI site

首先,我可以向您保证,WorkbookFactory和XSSFWorkbook类包含在POI-OOXML 3.13罐中,如POI站点上所述

$ unzip -l .m2/repository/org/apache/poi/poi-ooxml/3.13/poi-ooxml-3.13.jar | grep WorkbookFactory
  6041  2015-09-22 00:22   org/apache/poi/ss/usermodel/WorkbookFactory.class

However, as the exception you have posted makes clear, your code will never work for XLSX files, it needs changing. Well, for that and some other issues too... eg Don't use an InputStream if you have a File

但是,正如您发布的例外情况所表明的那样,您的代码永远不会适用于XLSX文件,需要更改。那么,对于那个以及其他一些问题......例如,如果你有一个文件,请不要使用InputStream

Your code should instead be more like:

你的代码应该更像是:

import java.io.File;
import org.apache.poi.ss.usermodel.*;

public static Sheet getExcelSheet(String fileName, int sheetIndex) throws IOException {
   File file = new File(fileName);
   Workbook workbook = WorkbookFactory.create(file);
   return workbook.getSheetAt(sheetIndex);
}

That will work for both xls and xlsx files, and will be lower memory than using an input stream. As long as you tell maven to depend on the poi-ooxml jar, you'll get all the other dependencies you need automatically

这对xls和xlsx文件都有效,并且比使用输入流的内存更低。只要您告诉maven依赖于poi-ooxml jar,您将获得所需的所有其他依赖项

#2


0  

The XSSFWorkbook presents in 3.11 version of Apache POI.

XSSFWorkbook提供了3.11版本的Apache POI。

Source code of apache POI

apache POI的源代码

Try to use this version of apache poi.

尝试使用这个版本的apache poi。

Also, it still present in trunk:

此外,它仍然存在于主干:

Source code of apache POI

apache POI的源代码

I believe the issue is with you maven local storage. try to remove poi folederes from maven local repository and redownload dependencies.

我相信问题在于maven本地存储。尝试从maven本地存储库中删除poi folederes并重新下载依赖项。

Also try to not forget to change all call from HSSFWorkbook to XSSFWorkbook, because XLSX files opens via XSSFWorkbook only.

另外,请不要忘记将所有来自HSSFWorkbook的呼叫更改为XSSFWorkbook,因为XLSX文件仅通过XSSFWorkbook打开。