如何使用 POI SS 打开 .xlsx 文件?

发布于 2024-11-04 04:36:17 字数 931 浏览 0 评论 0原文

我正在尝试使用 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();

我收到此错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException

我将 xbean.jar 添加到我的库和运行时库中。

我该如何解决这个异常?

谢谢 !

I am trying to open .xlsx files with POI SS with this code (taken from 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.

how can I resolve this exception?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏闹i 2024-11-11 04:36:17

第一:修复异常

有两种解决方案:

  1. 正如 Gagravarr 已经提到的:您需要 dom4j 来修复异常。
  2. 正如 Jon 已经提到的:您必须更新依赖项,因此您不再需要 dom4j。

如果您使用 Maven,则可以添加必要的依赖项:(也许可以在以下位置检查较新的版本: Maven 存储库: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>

然后:打开文件

如果您已修复异常,则可以使用以下代码打开 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 ...

更多提示,

  • 例如Gagravarr,我还建议使用文件而不是文件输入流。
  • 如果您想打开某个工作表,可以使用 workbook.getSheet(String name);
  • 如果您不知道根据您的项目的文件的相对路径,您可以使用 < code>System.out.println("相对路径:" + System.getProperty("user.dir"));

问候, winklerrr

First: Fix the Exception

There are two solutions:

  1. As Gagravarr already mentioned: you need dom4j to fix your exception.
  2. As Jon already mentioned: you have to update your dependencies, so you don't need dom4j anymore.

If you're using Maven, you can add the necessary dependencies with: (Maybe check for newer versions at: 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:

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"));

Regards, winklerrr

酒与心事 2024-11-11 04:36:17

我没有分析你的错误信息,但是看到代码后,我发现有些地方不正确。
Wookbook 不适用于 *.xlsx(Office 2007 及更高版本)文件。所以你必须使用XSSFWorkbook。您可能需要将 wb 初始化更改为:

XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);

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:

XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);
睡美人的小仙女 2024-11-11 04:36:17

这可以帮助你。

InputStream is = new FileInputStream(pathOfYourXlsxFile);
                XSSFWorkbook workbook = new XSSFWorkbook(is);
                //Get first sheet from the workbook
                XSSFSheet sheet = workbook.getSheetAt(0);

对于您的异常,您必须将 dom4j.jar 放入类路径中。您可以在此处找到它

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

长安忆 2024-11-11 04:36:17

您需要 dom4j,这就是异常告诉您的内容

您可能需要查看 组件 页面列出依赖项的 POI 网站

另外,由于您有一个 File,因此可以直接用它打开,而不是通过 InputStreamApache POI 常见问题解答中有一个部分,基本上使用文件比通过流缓冲整个内容更快、占用的内存更少!

You need dom4j, that's what the exception is telling you

You might want to look at the components page on the POI website which lists the dependencies

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!

不爱素颜 2024-11-11 04:36:17

我意识到该线程已经过时了,但今天它让我找到了同一问题所需的答案。

上面建议的页面上的文档(即包括 dom4j.jar)表示不再需要:

“OOXML jar 过去需要 DOM4J,但代码现在已更改为使用 JAXP,并且不需要额外的 dom4j jar。”

我发现包括以下内容可以解决问题:

poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
xmlbeans-2.6.0.jar

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:

"The OOXML jars used to require DOM4J, but the code has now been changed to use JAXP and no additional dom4j jars are required."

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文