【问题标题】:java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl.getTrList()Ljava/util/List while using Apache POI libraryjava.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl.getTrList()Ljava/util/List 同时使用 Apache POI 库
【发布时间】:2014-11-10 23:16:37
【问题描述】:

我正在尝试编写一段代码来读取 docx 文件,但由于某些奇怪的原因它失败了。

在这一行失败:

XWPFDocument document = new XWPFDocument(new FileInputStream(filepath));

堆栈跟踪:

Exception in thread "main" java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl.getTrList()Ljava/util/List;
    at org.apache.poi.xwpf.usermodel.XWPFTable.<init>(XWPFTable.java:106)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:151)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:124)
    at helper.JavaHelper.readTableDataFull(JavaHelper.java:84)
    at helper.JavaHelper.main(JavaHelper.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我有那些罐子:

  • dom4j-1.6.1.jar
  • xmlbeans-2.3.0.jar
  • poi-3.9-20121203.jar
  • poi-scratchpad-3.9-20121203.jar
  • poi-ooxml-3.9-20121203.jar
  • ooxml-schemas-1.0.jar

看起来它应该对我有用(我也尝试过使用较新的 poi 库 - 同样的错误)。

有什么想法吗?

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    快速浏览一下Grepcode 会发现v1.1ooxml-schemas 支持缺少的方法。 v1.0 似乎也不包含这样的方法(它只包含一个getTrArray() 方法)。

    因此您可能希望使用ooxml-schemas-1.1.jar 而不是ooxml-schemas-1.0.jar

    这是您至少应该使用的版本的 maven 依赖项:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.1</version>
    </dependency>
    

    【讨论】:

    • 工作!非常感谢。
    【解决方案2】:

    引用Apache POI FAQ page

    问:我正在使用 poi-ooxml-schemas jar,但我的代码因“java.lang.NoClassDefFoundError: org/openxmlformats/schemas/something”而失败强>

    要使用新的 OOXML 文件格式,POI 需要一个包含由 XMLBeans 编译的文件格式 XSD 的 jar。这些 XSD 一旦编译成 Java 类,就存在于 org.openxmlformats.schemas 命名空间中。

    有两个可用的 jar 文件,如组件概述部分所述。所有模式的完整 jar 是 ooxml-schemas-1.1.jar,目前大约 15mb。较小的 poi-ooxml-schemas jar 只有大约 4mb。不过,后一个 jar 文件只包含常用的部分。

    许多用户选择使用较小的 poi-ooxml-schemas jar 来节省空间。但是,poi-ooxml-schemas jar 仅包含通常使用的 XSD 和类,如单元测试所标识。每隔一段时间,您可能会尝试使用未包含在最小 poi-ooxml-schemas jar 中的部分文件格式。在这种情况下,您应该切换到完整的 ooxml-schemas-1.1.jar。从长远来看,您可能还希望提交一个使用 XSD 额外部分的新单元测试,以便将来的 poi-ooxml-schemas jar 包含它们。

    因此,正如常见问题解答所解释的,在短期内将小的 poi-ooxml-schemas jar 替换为较大的 ooxml-schemas jar。从长远来看,向 Apache POI 提交一个 junit 测试,该测试使用您想要使用的模式 jar 的位,然后它们将出现在下一个小 poi-ooxml-schemas jar 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2023-03-24
      • 2017-12-24
      相关资源
      最近更新 更多