I am trying to read a file from a .xlsx
file using java. But I still get errors. I already corrected the HSSF
to XSSF
so it is able to read past 2007 version of excel. The code crashes when instantiating the workbook. Here is the code:
我正在尝试使用java从.xlsx文件中读取一个文件。但我还是会犯错误。我已经将HSSF更正为XSSF,以便它能够读取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$1.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
3 个解决方案
#1
10
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。xmlbeans-2.3.0 jar。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 DocumentException
to be created, but hopefully that will tell you what's really wrong with your spreadsheet (if anything).
所以你的电子表格可能有问题。在类路径中放置Dom4J只会创建DocumentException,但希望这会告诉您电子表格的真正问题(如果有的话)。
#2
2
You should include dom4j-1.6.1.jar file
你应该包括dom4j-1.6.1。jar文件
#3
0
simply paste your poi libs in your web-inf lib directory if you are working in eclipse IDE.
如果在eclipse IDE中工作,只需将poi lib粘贴到web-inf lib目录中。
#1
10
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。xmlbeans-2.3.0 jar。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 DocumentException
to be created, but hopefully that will tell you what's really wrong with your spreadsheet (if anything).
所以你的电子表格可能有问题。在类路径中放置Dom4J只会创建DocumentException,但希望这会告诉您电子表格的真正问题(如果有的话)。
#2
2
You should include dom4j-1.6.1.jar file
你应该包括dom4j-1.6.1。jar文件
#3
0
simply paste your poi libs in your web-inf lib directory if you are working in eclipse IDE.
如果在eclipse IDE中工作,只需将poi lib粘贴到web-inf lib目录中。