I am trying to open .xlsx files with POI SS with this code (taken from http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook):
我试图使用此代码打开带有POI SS的.xlsx文件(取自http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook):
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
and I get this error message:
我收到此错误消息:
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
I add the xbean.jar to my library and to my run-time libraries.
我将xbean.jar添加到我的库和我的运行时库。
how can I resolve this exception?
我该如何解决这个异常?
Thanks !
5 个解决方案
#1
3
First: Fix the Exception
There are two solutions:
有两种解决方案:
- As Gagravarr already mentioned: you need dom4j to fix your exception.
- As Jon already mentioned: you have to update your dependencies, so you don't need dom4j anymore.
正如Gagravarr已经提到的:你需要dom4j来修复你的异常。
正如Jon已经提到过的:你必须更新你的依赖项,所以你不再需要dom4j了。
If you're using Maven, you can add the necessary dependencies with: (Maybe check for newer versions at: Maven Repository: org.apache.poi)
如果你正在使用Maven,你可以添加必要的依赖项:(也许在以下位置检查更新的版本:Maven Repository:org.apache.poi)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
Then: Open the File
If you've fixed the exception, you can open your file.xlsx
file with the following code:
如果您已修复异常,则可以使用以下代码打开file.xlsx文件:
String path = "Relative/Path/To/Your/File/file.xlsx";
File file = new File(path);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
// Use your sheet ...
Further tips
- Like Gagravarr, I also recommend to use a file instead of a file input stream.
- If you want to open a certain sheet you can use
workbook.getSheet(String name);
- If you don't know the relative path to your file according to your project, you can easily check it with
System.out.println("Relative path: " + System.getProperty("user.dir"));
与Gagravarr一样,我也建议使用文件而不是文件输入流。
如果要打开某个工作表,可以使用workbook.getSheet(String name);
如果根据项目不知道文件的相对路径,可以使用System.out.println轻松检查它(“Relative path:”+ System.getProperty(“user.dir”));
Regards, winklerrr
#2
2
I haven't analyzed your error message, but after seeing the code, I see that there is something not correct.
The Wookbook is not work with *.xlsx (Office 2007 and after) files. So you have to use the XSSFWorkbook. You may want to change the wb initialization to:
我没有分析你的错误信息,但看到代码后,我发现有些东西不正确。 Wookbook不适用于* .xlsx(Office 2007及更高版本)文件。所以你必须使用XSSFWorkbook。您可能希望将wb初始化更改为:
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);
#3
2
You need dom4j, that's what the exception is telling you
你需要dom4j,这就是异常告诉你的
You might want to look at the components page on the POI website which lists the dependencies
您可能希望查看POI网站上列出依赖项的组件页面
Also, since you have a File
, open directly with that, don't go via an InputStream
. There's a section in the Apache POI FAQ on that, basically using a file is quicker and lower memory than buffering the whole thing via a stream!
此外,由于您有一个文件,直接用它打开,不要通过InputStream。 Apache POI常见问题解答中有一节,基本上使用文件比通过流缓冲整个内容更快更低内存!
#4
1
This can help you.
这可以帮到你。
InputStream is = new FileInputStream(pathOfYourXlsxFile);
XSSFWorkbook workbook = new XSSFWorkbook(is);
//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
For your Exception you have to put dom4j.jar to your classpath.You can find it here
对于您的Exception,您必须将dom4j.jar放到类路径中。您可以在此处找到它
#5
0
I realize the thread is old, but it led me today to the answer I needed for the same problem.
我意识到线程已经过时了,但它今天引导我找到了同样问题所需的答案。
The docs on the page suggested above (i.e include dom4j.jar) say that's no longer necessary:
上面提到的页面上的文档(即包括dom4j.jar)表示不再需要:
"The OOXML jars used to require DOM4J, but the code has now been changed to use JAXP and no additional dom4j jars are required."
“OOXML jar用于需要DOM4J,但现在代码已经改为使用JAXP,并且不需要额外的dom4j jar。”
I found that including the following resolved the problem:
我发现包括以下内容解决了这个问题:
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
xmlbeans-2.6.0.jar
#1
3
First: Fix the Exception
There are two solutions:
有两种解决方案:
- As Gagravarr already mentioned: you need dom4j to fix your exception.
- As Jon already mentioned: you have to update your dependencies, so you don't need dom4j anymore.
正如Gagravarr已经提到的:你需要dom4j来修复你的异常。
正如Jon已经提到过的:你必须更新你的依赖项,所以你不再需要dom4j了。
If you're using Maven, you can add the necessary dependencies with: (Maybe check for newer versions at: Maven Repository: org.apache.poi)
如果你正在使用Maven,你可以添加必要的依赖项:(也许在以下位置检查更新的版本:Maven Repository:org.apache.poi)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
Then: Open the File
If you've fixed the exception, you can open your file.xlsx
file with the following code:
如果您已修复异常,则可以使用以下代码打开file.xlsx文件:
String path = "Relative/Path/To/Your/File/file.xlsx";
File file = new File(path);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
// Use your sheet ...
Further tips
- Like Gagravarr, I also recommend to use a file instead of a file input stream.
- If you want to open a certain sheet you can use
workbook.getSheet(String name);
- If you don't know the relative path to your file according to your project, you can easily check it with
System.out.println("Relative path: " + System.getProperty("user.dir"));
与Gagravarr一样,我也建议使用文件而不是文件输入流。
如果要打开某个工作表,可以使用workbook.getSheet(String name);
如果根据项目不知道文件的相对路径,可以使用System.out.println轻松检查它(“Relative path:”+ System.getProperty(“user.dir”));
Regards, winklerrr
#2
2
I haven't analyzed your error message, but after seeing the code, I see that there is something not correct.
The Wookbook is not work with *.xlsx (Office 2007 and after) files. So you have to use the XSSFWorkbook. You may want to change the wb initialization to:
我没有分析你的错误信息,但看到代码后,我发现有些东西不正确。 Wookbook不适用于* .xlsx(Office 2007及更高版本)文件。所以你必须使用XSSFWorkbook。您可能希望将wb初始化更改为:
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);
#3
2
You need dom4j, that's what the exception is telling you
你需要dom4j,这就是异常告诉你的
You might want to look at the components page on the POI website which lists the dependencies
您可能希望查看POI网站上列出依赖项的组件页面
Also, since you have a File
, open directly with that, don't go via an InputStream
. There's a section in the Apache POI FAQ on that, basically using a file is quicker and lower memory than buffering the whole thing via a stream!
此外,由于您有一个文件,直接用它打开,不要通过InputStream。 Apache POI常见问题解答中有一节,基本上使用文件比通过流缓冲整个内容更快更低内存!
#4
1
This can help you.
这可以帮到你。
InputStream is = new FileInputStream(pathOfYourXlsxFile);
XSSFWorkbook workbook = new XSSFWorkbook(is);
//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
For your Exception you have to put dom4j.jar to your classpath.You can find it here
对于您的Exception,您必须将dom4j.jar放到类路径中。您可以在此处找到它
#5
0
I realize the thread is old, but it led me today to the answer I needed for the same problem.
我意识到线程已经过时了,但它今天引导我找到了同样问题所需的答案。
The docs on the page suggested above (i.e include dom4j.jar) say that's no longer necessary:
上面提到的页面上的文档(即包括dom4j.jar)表示不再需要:
"The OOXML jars used to require DOM4J, but the code has now been changed to use JAXP and no additional dom4j jars are required."
“OOXML jar用于需要DOM4J,但现在代码已经改为使用JAXP,并且不需要额外的dom4j jar。”
I found that including the following resolved the problem:
我发现包括以下内容解决了这个问题:
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
xmlbeans-2.6.0.jar