简体   繁体   English

无法在 Apache POI 中导入 XSSF

[英]Cannot import XSSF in Apache POI

I am referencing the version 3.7 of the Apache POI and I am getting a "cannot be resolved" error when I do:我引用的是 Apache POI 的 3.7 版,并且在执行此操作时出现“无法解决”错误:

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

Other import statements that reference POI DO NOT give me errors, such as:其他引用 POI 的导入语句不会给我错误,例如:

import org.apache.poi.ss.usermodel.*;

Any ideas??有任何想法吗??

For OOXML to work you need the POI-OOXML jar which is separately packaged from the POI jar.要使 OOXML 工作,您需要 POI-OOXML jar,它与 POI jar 分开包装。

Download the POI-OOXML jar from the following location -从以下位置下载 POI-OOXML jar -

http://repo1.maven.org/maven2/org/apache/poi/poi-ooxml/3.11/poi-ooxml-3.11.jar http://repo1.maven.org/maven2/org/apache/poi/poi-ooxml/3.11/poi-ooxml-3.11.Z68995FCBF432492D15484D240A

For Maven2 add the below dependency -对于 Maven2 添加以下依赖项 -

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

The classes for the OOXML file formats (such as XSSF for.xlsx) are in a different Jar file. OOXML 文件格式(例如 XSSF for.xlsx)的类位于不同的 Jar 文件中。 You need to include the poi-ooxml jar in your project, along with the dependencies for it您需要在项目中包含 poi-ooxml jar 及其依赖项

You can get a list of all the components and their dependencies on the POI website here .您可以在此处获取 POI 网站上所有组件及其依赖项的列表。

What you probably want to do is download the 3.11 binary package , grab the poi-ooxml jar from it, and the dependencies in the ooxml-lib directory.您可能想要做的是下载3.11 二进制 package ,从中获取poi-ooxml jar 以及ooxml-lib目录中的依赖项。 Import these into your project and you'll be sorted.将这些导入到您的项目中,您将被分类。

Alternately, if you use Maven, you can see here for the list of the artificats you'll want to depend on, but it'd want to be something like:或者,如果您使用 Maven,您可以在此处查看您想要依赖的人工制品列表,但它希望类似于:

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

The poi-ooxml maven dependency will pull in the main POI jar and the dependencies for you automatically. poi-ooxml maven 依赖项将自动为您引入主 POI jar 和依赖项。 If you want to work with the non-spreadsheet formats, you'd also want to depend on the poi-scratchpad artifact too, as detailed on the POI components page如果您想使用非电子表格格式,您还需要依赖poi-scratchpad工件,如POI 组件页面中所述

I added below contents in app "build.gradle"我在应用程序“build.gradle”中添加了以下内容

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

If you use Maven:如果您使用 Maven:

poi => poi-ooxml in artifactId poi => artifactId 中的 poi-ooxml

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

Problem: While importing the " org.apache.poi.xssf.usermodel.XSSFWorkbook"class showing an error in eclipse.问题:导入“org.apache.poi.xssf.usermodel.XSSFWorkbook”类时,在 eclipse 中显示错误。

Solution: Use This maven dependency to resolve this problem:解决方案:使用这个 maven 依赖来解决这个问题:

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

-Hari Krishna Neela ——哈里·克里希纳·尼拉

I Got the Solution Guys我得到了解决方案伙计们

You need to keep some points in your mind.你需要记住一些要点。

  1. There are two different dependency one is ( poi ) & other dependency is ( poi- ooxml ) but make sure you must use poi-ooxml dependency in your code.有两种不同的依赖项,一个是 ( poi ),另一个依赖项是 (poi- ooxml ),但请确保您必须在代码中使用 poi-ooxml 依赖项。

  2. Just Add the following dependency in pom.xml & Save it.只需在 pom.xml 中添加以下依赖项并保存即可。

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

3.After you have saved the pom.xml then you need to try a small thing,Use (.) operator will try to import/fetch it & finally will not see any sort of error because now it has imported that thing in your package. 3.保存 pom.xml 之后,您需要尝试一个小东西,使用 (.) 运算符将尝试导入/获取它,最后不会看到任何错误,因为现在它已经在您的 package 中导入了该东西.

Sample Code to Understand Better !!示例代码以更好地理解!

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

I tried my best to give you the solution, If you face any issue comment here i will try我尽力为您提供解决方案,如果您在这里遇到任何问题评论,我会尝试
to solve it.解决它。

Keep Learning Guys !!继续学习伙计们!!

1) imported all the JARS from POI folder 2) Imported all the JARS from ooxml folder which a subdirectory of POI folder 3) Imported all the JARS from lib folder which is a subdirectory of POI folder 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();
}​

I had the same problem, so I dug through the poi-3.17.jar file and there was no xssf package inside.我遇到了同样的问题,所以我翻遍了 poi-3.17.jar 文件,里面没有 xssf package。

I then went through the other files and found xssf int the poi-ooxml-3.17.jar然后我浏览了其他文件,发现 xssf int poi-ooxml-3.17.jar

So it seems the solutions is to add所以似乎解决方案是添加

poi-ooxml-3.17.jar

to your project, as that seems to make it work (for me at least)到你的项目,因为这似乎使它工作(至少对我来说)

After trying multiple things,what really worked was: 1. downloading "poi" and "poi-ooxml" manually 2.Adding these d/w jars into "Maven Dependencies"在尝试了多件事之后,真正奏效的是:1.手动下载“poi”和“poi-ooxml” 2.将这些d/w jars添加到“Maven Dependencies”中

You did not described the environment, anyway, you should download apache poi libraries.你没有描述环境,反正你应该下载apache poi库。 If you are using eclipse, right click on your root project, so properties and in java build path add external jar and import in your project those libraries:如果您使用的是 eclipse,请右键单击您的根项目,然后在属性和 java 构建路径中添加外部 jar 并在您的项目中导入这些库:

xmlbeans-2.6.0; xmlbeans-2.6.0; poi-ooxml-schemas-... ; poi-ooxml-schemas-...; poi-ooxml-... ; poi-ooxml-... ; poi-.... ;点-....;

I needed the following files for my implementation:我的实现需要以下文件:

  • poi-ooxml-schemas-3.14.20160307.jar poi-ooxml-schemas-3.14.20160307.jar
  • commons-codec-1.10.jar (this was in "lib" folder of the zip file you get from apache) commons-codec-1.10.jar(这是在您从 apache 获得的 zip 文件的“lib”文件夹中)
  • curvesapi-1.03.jar (in "ooxml-lib" folder) curveapi-1.03.jar(在“ooxml-lib”文件夹中)
  • poi-3.14-20160307.jar poi-3.14-20160307.jar
  • poi-ooxml-3.14-20160307.jar poi-ooxml-3.14-20160307.jar
  • xmlbeans-2.6.0.jar (in "ooxml-lib" folder) xmlbeans-2.6.0.jar(在“ooxml-lib”文件夹中)

(though honestly, I'm not completely sure they are all necessary...) It's a little confusing because they are packaged that way. (虽然老实说,我不完全确定它们都是必需的......)这有点令人困惑,因为它们是这样包装的。 I needed to place them manually in my own "lib" folder and then add the references...我需要将它们手动放置在我自己的“lib”文件夹中,然后添加引用...

Maven always seems to download more than I need, so I always place libaries/dlls and things like that manually. Maven 似乎总是下载比我需要的多,所以我总是手动放置库/dll 和类似的东西。

Actual for 06.11.2021 days: 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>

If you use Maven:如果您使用 Maven:

Add the poi and the poi-ooxml dependency to make it work:)添加 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.

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