无法在Apache POI中导入XSSF

147

我正在引用Apache POI的3.7版本,但在执行以下代码时出现“无法解析”的错误:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

其他引用POI的导入语句不会给我带来错误,例如:

import org.apache.poi.ss.usermodel.*;
任何想法吗?

我遇到了相同的问题,似乎根源在于 poi-3.whatever.jar 文件中出于某些原因未包含 xssf。 - Mr Redstoner
14个回答

235

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Clint L

39
OOXML文件格式(如.xlsx)的类位于不同的Jar文件中。您需要在项目中包含poi-ooxml jar及其依赖项。您可以在POI网站上获取所有组件及其依赖项的列表此处。您可能想要做的是下载3.11二进制包,从中获取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组件页面


20

我在应用程序的 "build.gradle" 文件中添加了以下内容。

implementation 'org.apache.poi:poi:4.0.0'
implementation 'org.apache.poi:poi-ooxml:4.0.0'

它真的很有帮助。 - Abror Esonaliev

8
如果您使用Maven:

poi => 在artifactId中使用poi-ooxml

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

我建议使用POI 3.12 final (3.12)而不是beta版本。 - Gagravarr

7

我有解决方案

你需要记住以下几点:

  1. 有两个不同的依赖项,一个是(poi),另一个是(poi-ooxml),但请确保在代码中使用poi-ooxml依赖项。

  2. 只需在pom.xml中添加以下依赖项并保存即可。

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

3. 保存完pom.xml文件后,您需要尝试一些小事情,使用(.)操作符将会尝试导入/提取它,并且最终不会看到任何错误,因为现在它已经在您的包中导入了那个东西。

示例代码以更好地理解!!

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();   
}  
}

我尽力为您提供了解决方案,如果您遇到任何问题,请在此处评论,我将尝试解决它。
继续学习吧!

6
问题:在eclipse中导入“org.apache.poi.xssf.usermodel.XSSFWorkbook”类时出现错误。
解决方案:使用以下Maven依赖项来解决此问题:
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.15</version>
</dependency>

-哈里·克里希纳·尼拉
(注:该文本为一人名)

3

截至2021年6月11日的实际情况:

<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>

这对我有用,谢谢你。 - undefined

2

1)从POI文件夹中导入了所有的JAR包。 2)从POI文件夹的子目录ooxml中导入了所有的JAR包。 3)从POI文件夹的子目录lib中导入了所有的JAR包。

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();
}​

行没有导入...请问您能分享一下 jar 文件链接吗? - demo

2

我曾经遇到过同样的问题,因此我查看了poi-3.17.jar文件,但是没有找到xssf包。

然后我查看了其他文件,并在poi-ooxml-3.17.jar中找到了xssf。

所以解决方法就是添加:

poi-ooxml-3.17.jar

添加到你的项目中,因为这似乎可以使其工作(至少对我来说是这样)


1
尝试了多种方法后,真正有效的方法是: 1. 手动下载“poi”和“poi-ooxml”。 2. 将这些下载的文件添加到“Maven Dependencies”中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接