首页 > 解决方案 > 未找到 CTFont.addNewFamily() 函数

问题描述

包括以下库:

..\lib\ooxml-schemas-1.4.jar;..\lib\poi-ooxml-5.0.0.jar;..\lib\poi-5.0.0.jar;..\lib\ooxml-lib\ xmlbeans-4.0.0.jar;..\lib\lib\commons-collections4-4.4.jar;..\lib\commons-compress-1.20.jar

我正在尝试创建和 excel 文件并写入其中。它编译正常。但是,当我运行时,会出现“没有这样的方法错误”。我注意到 CTFont.class 存在于 ooxml-schemas-1.4.jar 中。想知道我能找到哪个版本,CTFont.addNewFamily()。

Exception in thread "main" java.lang.NoSuchMethodError: 'org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontFamily org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()'
        at org.apache.poi.xssf.usermodel.XSSFFont.setFamily(XSSFFont.java:635)
        at org.apache.poi.xssf.usermodel.XSSFFont.setFamily(XSSFFont.java:647)
        at org.apache.poi.xssf.model.StylesTable.createDefaultFont(StylesTable.java:765)
        at org.apache.poi.xssf.model.StylesTable.initialize(StylesTable.java:716)
        at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:130)
        at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:94)
        at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:591)
        at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:500)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:465)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:255)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:249)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:237)
        at WriteDataToExcel.main(WriteDataToExcel.java)

标签: java

解决方案


在类路径中包含 poi-ooxml-full-5.0.0.jar 和 poi-ooxml-lite-5.0.0.jar 并删除 ooxml-schemas-1.4.jar。

我从以下链接得到了答案: java.lang.NoSuchMethodError: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont.addNewFamily()

根据上面的链接,

5.0.0 版的 apache poi 不能再使用 ooxml-schemas-1.4.jar。它需要 poi-ooxml-lite...jar 或 poi-ooxml-full...jar。所以当使用 apache poi 5.0.0 时,请确保类路径中没有 ooxml-schemas-1.4.jar。


推荐阅读