简体   繁体   English

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

[英]Apache Poi 3.13 can't find classes to open XLSX files

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的文件,但是我无法在源代码中找到WorkbookFactoryXSSFWorkbook来读取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. 提前致谢。

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 首先,我可以向您保证WorkbookFactoryXSSFWorkbook类包含在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. 但是,正如您发布的例外情况所表明的那样,您的代码永远不会适用于XLSX文件,它需要更改。 Well, for that and some other issues too... eg Don't use an InputStream if you have a File 那么,对于那个和其他一些问题......例如, 如果你有一个文件,请不要使用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. 这对xlsxlsx文件都有效,并且比使用输入流的内存更低。 As long as you tell maven to depend on the poi-ooxml jar, you'll get all the other dependencies you need automatically 只要您告诉maven依赖于poi-ooxml jar,您将获得所需的所有其他依赖项

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. 我相信问题在于你maven本地存储。 try to remove poi folederes from maven local repository and redownload dependencies. 尝试从maven本地存储库中删除poi folederes并重新下载依赖项。

Also try to not forget to change all call from HSSFWorkbook to XSSFWorkbook , because XLSX files opens via XSSFWorkbook only. 另外,请不要忘记将所有来自HSSFWorkbook呼叫HSSFWorkbookXSSFWorkbook ,因为XLSX文件仅通过XSSFWorkbook打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM