使用 java Apache POI 3.9 Eclipse 从 excel 文件 .xlsx 中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18282378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:01:03  来源:igfitidea点击:

Read from excel file .xlsx using java Apache POI 3.9 Eclipse

javaexcelapacheapache-poi

提问by Jenny

I am trying to read a file from a .xlsxfile using java. But I still get errors. I already corrected the HSSFto XSSFso it is able to read past 2007 version of excel. The code crashes when instantiating the workbook. Here is the code:

我正在尝试.xlsx使用 java从文件中读取文件。但我仍然遇到错误。我已经更正了HSSFXSSF以便它能够读取 2007 年版本的 excel。实例化工作簿时代码崩溃。这是代码:

package excelread;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File excel =  new File ("C:/Users/Leah-Dina/Desktop/LogFile.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet ws = wb.getSheet("Input");

        int rowNum = ws.getLastRowNum() + 1;
        int colNum = ws.getRow(0).getLastCellNum();
        String [][] data = new String [rowNum] [colNum];

        for(int i = 0; i <rowNum; i++){
            XSSFRow row = ws.getRow(i);
                for (int j = 0; j < colNum; j++){
                    XSSFCell cell = row.getCell(j);
                    String value = cell.toString();
                    data[i][j] = value;
                    System.out.println ("the value is " + value);
                }
        }

    }
}

Here you can see the error message I get: Seems like everything is imported and I have no idea whats wrong.

在这里您可以看到我收到的错误消息:似乎所有内容都已导入,但我不知道出了什么问题。

 Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
        at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:154)
        at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
        at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
        at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
        at excelread.ReadExcel.main(ReadExcel.java:21)
    Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
        at java.net.URLClassLoader.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 8 more

采纳答案by rgettman

First, make sure that all libraries that Apache POI depends on are on your classpath. In this case, you are certainly missing Dom4J (dom4j-1.6.1.jar). Possibly you may be missing other libraries, such as stax-api-1.0.1.jar, xmlbeans-2.3.0.jar, and poi-ooxml-schemas-3.9.jar. All necessary libraries are included in the distribution that is downloadable from the Apache POI website.

首先,确保 Apache POI 所依赖的所有库都在您的类路径中。在这种情况下,您肯定缺少 Dom4J (dom4j-1.6.1.jar)。您可能缺少其他库,例如 stax-api-1.0.1.jar、xmlbeans-2.3.0.jar 和 poi-ooxml-schemas-3.9.jar。所有必需的库都包含在可从Apache POI 网站下载的发行版中。

Line 21 appears to be this line:

第 21 行似乎是这一行:

XSSFWorkbook wb = new XSSFWorkbook(fis);

So there may be a problem with your spreadsheet. Putting Dom4J on your classpath will only allow the DocumentExceptionto be created, but hopefully that will tell you what's really wrong with your spreadsheet (if anything).

所以你的电子表格可能有问题。将 Dom4J 放在您的类路径上只会允许DocumentException创建 ,但希望这会告诉您电子表格的真正问题(如果有的话)。

回答by santhosh

simply paste your poi libs in your web-inf lib directory if you are working in eclipse IDE.

如果您在 Eclipse IDE 中工作,只需将您的 poi 库粘贴到您的 web-inf lib 目录中。

回答by Kundan

You should include dom4j-1.6.1.jar file

你应该包含 dom4j-1.6.1.jar 文件