Java 使用 Apache POI 的程序出现奇怪的异常

Java Program using Apache POI giving me weird Exceptions

我在启动我的项目时遇到了一些严重的困难。我有以下代码:

FileInputStream file = new FileInputStream(new File("src/retestchecker/test_sheet.xlsx"));
//Get the workbook instance for XLS file 
XSSFWorkbook workbook = new XSSFWorkbook(file);

//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(0);
Cell x = row.getCell(3);

System.out.println(x);

我收到这个错误,我不确定它是什么意思:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at retestchecker.RetestChecker.main(RetestChecker.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
Java Result: 1

我正在使用 Netbeans 和 2015 年 5 月 11 日发布的最新版本的 Apache POI。

错误所指的第23行是这一行:

XSSFWorkbook workbook = new XSSFWorkbook(file);

我做错了什么?

这意味着编译器找不到名为 org.apache.xmlbeans.XmlException 的 class 你必须找到 apache poi 的哪些 .jar 文件定义它并将它们添加到你的项目的 classpath.

您缺少 xmlbeans jar(可能还有更多)

A​​pache POI 有一个 handy page describing all of the components and their dependencies,或者如果您使用 Maven,它都会为您处理好

在二进制版本中,您会在 ooxml-lib 子目录中找到 xmlbeans。将它和任何其他罐子 needed for the components you're using 添加到您的类路径