无法使用 Apache POI 读取 Excel [英] Unable to read Excel using Apache POI

查看:47
本文介绍了无法使用 Apache POI 读取 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI 读取 excel,但它一直为 XSSFWorkbook 类定义找到错误提供错误.我使用了不同版本的 Apache-poi jar 库(即 4.1 、 4.0 和 3.12 )似乎都没有解决这个错误.这是当前导入的库的屏幕截图在此处输入图片描述代码内部有什么问题?

I'm reading excel using Apache POI but it keeps giving error for XSSFWorkbook class definition found error. I used different versions of Apache-poi jar libraries (i.e 4.1 , 4.0 and 3.12) none of them seem to fix this error. Here's a screenshot of libraries currently imported enter image description here Whats wrong inside the code ?

try {   
        File fileSrc = new File("C://Eclipse-Workspace//TestData//TestDataSet.xlsx");
            FileInputStream fis = new FileInputStream(fileSrc); 
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFCell cellData = sheet.getRow(rowNo).getCell(colNo);
workbook.close();
return cellData.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

显示错误:

 java.lang.NoClassDefFoundError: 
    org/apache/poi/ss/usermodel/WorkbookFactory
    at Utilities.DataAccessorExcel.excelReader(DataAccessorExcel.java:33)
    at TestCases.LoginTest.verifyLogin(LoginTest.java:66)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 
    Method)
    atjava.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMe 
    thodAccessorImpl.java:62)
    atjava.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Dele 
    gatingMethodAccessorImpl.java:43)at 
    java.base/java.lang.reflect.Method.invoke(Method.java:567)

推荐答案

你必须包含 poi jar 文件.它的版本将是 4.1.0.如果您使用的是 Maven pom.xml,请包含以下依赖项.

You have to include poi jar file. It's version will be 4.1.0. If you are using Maven pom.xml, include the following dependency.

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

如果您不使用 Maven,请从以下位置下载 jar 文件.

If you are not using Maven, download the jar files from the following locations.

http:///central.maven.org/maven2/org/apache/poi/poi/4.1.0/poi-4.1.0.jar

http://central.maven.org/maven2/org/apache/poi/poi-ooxml/4.1.0/poi-ooxml-4.1.0.jar

为了使用Apache POI读取excel文件,如果您不想使用Maven,则需要以下jar文件.

For reading an excel file with Apache POI, you need the following jar files if you do not want to use Maven.

  1. poi-ooxml-4.1.0.jar
  2. poi-ooxml-schemas-4.1.0.jar
  3. xmlbeans-3.1.0.jar
  4. commons-compress-1.18.jar
  5. curvesapi-1.06.jar
  6. poi-4.1.0.jar
  7. commons-codec-1.12.jar
  8. commons-collections4-4.3.jar
  9. commons-math3-3.6.1.jar

我在下面提供了一个关于如何使用的简短代码片段.

I provide below a brief code snippet about how to use.

FileInputStream file =
        new FileInputStream(
            new File(
                "testdata\\TestData_01.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) {
      Row row = rowIterator.next();
      Cell cell0 = row.getCell(0);
      if (cell0 != null) {
        System.out.println("First Column Data : "+cell0.getStringCellValue());
      }
      Cell cell1 = row.getCell(1);
      if (cell1 != null) System.out.println("Second Column Data : "+cell1.getStringCellValue());


    }

这篇关于无法使用 Apache POI 读取 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆