使用 XSSFWorkbook 创建 xlsx 文件,但在使用方法 createSheet("String") 时出错

Creating xlsx file using XSSFWorkbook, but getting error while using method createSheet("String")

我已尝试将所有需要的 jar 包含在参考库中。 本论坛也有类似的问题,我已经全部解决了,还是无法解决。

我的代码片段:

Workbook workbook = new XSSFWorkbook();
        CreationHelper createHelper = workbook.getCreationHelper();
        Sheet sheet = workbook.createSheet("Gene");
        Font headerFont = workbook.createFont();

All the jars that I am using

我收到以下错误:

异常堆栈跟踪:

  org.apache.poi.ooxml.POIXMLException: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
        at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:602)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:896)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:807)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:122)
        at DomParser.main(DomParser.java:18)
    Caused by: org.apache.poi.ooxml.POIXMLException: java.lang.reflect.InvocationTargetException
        at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:111)
        at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:587)
        ... 4 more
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
        at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:109)
        ... 5 more
    Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl.generatedSetterHelperImpl(Lorg/apache/xmlbeans/XmlObject;Ljavax/xml/namespace/QName;IS)Lorg/apache/xmlbeans/XmlObject;
        at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl.setColsArray(Unknown Source)
        at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.cleanColumns(ColumnHelper.java:66)
        at org.apache.poi.xssf.usermodel.helpers.ColumnHelper.<init>(ColumnHelper.java:46)
        at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentCreate(XSSFSheet.java:259)
        at org.apache.poi.xssf.usermodel.XSSFSheet.<init>(XSSFSheet.java:187)
        ... 11 more

您需要使用 xmlbeans-3.0.1 而不是 xmlbeans-5.1.3

从错误开始,错误是:

Caused by: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl.generatedSetterHelperImpl(Lorg/apache/xmlbeans/XmlObject;Ljavax/xml/namespace/QName;IS)Lorg/apache/xmlbeans/XmlObject;

我在 Maven 中央存储库中搜索了包含 class CTWorksheetImpl 的 jar: https://search.maven.org/search?q=fc:org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorksheetImpl

答案是 poi-ooxml-schemas,您已经有了,并且与 poi 的版本相同。看起来不错。然后我认为它必须是方法本身。参数来自 org.apache.xmlbeans 包,所以 xmlbeans 版本可能是错误的。

如何找到正确的版本?

与其手动收集 jar 文件,不如尝试 Maven 进行依赖项管理。在 Maven 项目中,只要说你想要 poi-ooxml 就足够了。这将自动以正确的版本递归地引入所有依赖项。

这是您可以在您的项目中使用的示例 Maven pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>poi-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

即使您不想(或不能)在您的项目中使用 Maven,您至少可以像我一样做一个单独的项目来找出依赖项。