[英]Cannot import XSSF in Apache POI
我引用的是 Apache POI 的 3.7 版,并且在执行此操作时出现“无法解决”错误:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
其他引用 POI 的导入语句不会给我错误,例如:
import org.apache.poi.ss.usermodel.*;
有任何想法吗??
要使 OOXML 工作,您需要 POI-OOXML jar,它与 POI jar 分开包装。
从以下位置下载 POI-OOXML jar -
对于 Maven2 添加以下依赖项 -
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
OOXML 文件格式(例如 XSSF for.xlsx)的类位于不同的 Jar 文件中。 您需要在项目中包含 poi-ooxml jar 及其依赖项
您可以在此处获取 POI 网站上所有组件及其依赖项的列表。
您可能想要做的是下载3.11 二进制 package ,从中获取poi-ooxml
jar 以及ooxml-lib
目录中的依赖项。 将这些导入到您的项目中,您将被分类。
或者,如果您使用 Maven,您可以在此处查看您想要依赖的人工制品列表,但它希望类似于:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
poi-ooxml maven 依赖项将自动为您引入主 POI jar 和依赖项。 如果您想使用非电子表格格式,您还需要依赖poi-scratchpad
工件,如POI 组件页面中所述
我在应用程序“build.gradle”中添加了以下内容
implementation 'org.apache.poi:poi:4.0.0'
implementation 'org.apache.poi:poi-ooxml:4.0.0'
如果您使用 Maven:
poi => artifactId 中的 poi-ooxml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
问题:导入“org.apache.poi.xssf.usermodel.XSSFWorkbook”类时,在 eclipse 中显示错误。
解决方案:使用这个 maven 依赖来解决这个问题:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
——哈里·克里希纳·尼拉
我得到了解决方案伙计们
你需要记住一些要点。
有两种不同的依赖项,一个是 ( poi ),另一个依赖项是 (poi- ooxml ),但请确保您必须在代码中使用 poi-ooxml 依赖项。
只需在 pom.xml 中添加以下依赖项并保存即可。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
3.保存 pom.xml 之后,您需要尝试一个小东西,使用 (.) 运算符将尝试导入/获取它,最后不会看到任何错误,因为现在它已经在您的 package 中导入了该东西.
示例代码以更好地理解!
package ReadFile;// package
import org.apache.poi.xssf.usermodel.XSSFWorkbook; // automatically added to your code after importing
public class Test
{
public static void Hello() // Method
{
XSSFWorkbook workbook = new XSSFWorkbook();
}
}
我尽力为您提供解决方案,如果您在这里遇到任何问题评论,我会尝试
解决它。
继续学习伙计们!!
1) 从 POI 文件夹中导入所有 JARS 2) 从 POI 文件夹的子目录 ooxml 文件夹中导入所有 JARS 3) 从 libF8 文件夹中导入所有 Z9327422E5DF141D06047C 文件夹的子目录
String fileName = "C:/File raw.xlsx";
File file = new File(fileName);
FileInputStream fileInputStream;
Workbook workbook = null;
Sheet sheet;
Iterator<Row> rowIterator;
try {
fileInputStream = new FileInputStream(file);
String fileExtension = fileName.substring(fileName.indexOf("."));
System.out.println(fileExtension);
if(fileExtension.equals(".xls")){
workbook = new HSSFWorkbook(new POIFSFileSystem(fileInputStream));
}
else if(fileExtension.equals(".xlsx")){
workbook = new XSSFWorkbook(fileInputStream);
}
else {
System.out.println("Wrong File Type");
}
FormulaEvaluator evaluator workbook.getCreationHelper().createFormulaEvaluator();
sheet = workbook.getSheetAt(0);
rowIterator = sheet.iterator();
while(rowIterator.hasNext()){
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()){
Cell cell = cellIterator.next();
//Check the cell type after evaluating formulae
//If it is formula cell, it will be evaluated otherwise no change will happen
switch (evaluator.evaluateInCell(cell).getCellType()){
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_FORMULA:
Not again
break;
case Cell.CELL_TYPE_BLANK:
break;
}
}
System.out.println("\n");
}
//System.out.println(sheet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
我遇到了同样的问题,所以我翻遍了 poi-3.17.jar 文件,里面没有 xssf package。
然后我浏览了其他文件,发现 xssf int poi-ooxml-3.17.jar
所以似乎解决方案是添加
poi-ooxml-3.17.jar
到你的项目,因为这似乎使它工作(至少对我来说)
在尝试了多件事之后,真正奏效的是:1.手动下载“poi”和“poi-ooxml” 2.将这些d/w jars添加到“Maven Dependencies”中
你没有描述环境,反正你应该下载apache poi库。 如果您使用的是 eclipse,请右键单击您的根项目,然后在属性和 java 构建路径中添加外部 jar 并在您的项目中导入这些库:
xmlbeans-2.6.0; poi-ooxml-schemas-...; poi-ooxml-... ; 点-....;
我的实现需要以下文件:
(虽然老实说,我不完全确定它们都是必需的......)这有点令人困惑,因为它们是这样包装的。 我需要将它们手动放置在我自己的“lib”文件夹中,然后添加引用...
Maven 似乎总是下载比我需要的多,所以我总是手动放置库/dll 和类似的东西。
2021 年 11 月 6 日的实际情况:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>
添加 poi 和 poi-ooxml 依赖项以使其工作:)
<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-ooxml.version}</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.