java - 未找到 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)
解决方案
在类路径中包含 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。
推荐阅读
- windows - PSWindowsUpdate 文件未找到异常
- java - java validator.validate() 方法需要更长的时间来验证
- python - 在 MacOS 上安装 requests python 库
- javascript - Polygon triangulation for globe
- swift - UIPasteboard 在后台返回 nil
- javascript - 模态关闭后显示按钮
- java - 让斯坦福 NLP 识别具有多个单词的命名实体
- scala - 如何在 playframework 模板中显示换行符?
- mysql - 如何优化这个 MYSQL 查询?(获得结果的巨大延迟)
- css - 在旧的 Mozilla 版本中隐藏滚动条,保留滚动条