POI版本从4.x升级到5.x后缺少方法

问题描述 投票:0回答:1

当我将POI版本从4.x升级到5.x时,出现方法缺失的错误;

    private static void setHeaderAndFooterXWPFRunStyle(XWPFRun r1, String font, int fontSize) {
        r1.setFontSize(fontSize);
        CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr();
        CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
        fonts.setAscii(font);
        fonts.setEastAsia(font);
        fonts.setHAnsi(font);
    }

缺少的方法是 isSetRPr() 和 getRFonts()

CTShd cTShd = pr.getShd();
STOnOff.TRUE

STOnOff.TRUE 和 getShd() 也被错过了

当我将依赖项从 5.x 更改为 4.x 时,一切正常

5.x的依赖

    implementation 'org.apache.poi:poi:5.2.3'
    implementation 'org.apache.poi:poi-ooxml:5.2.3'
    implementation 'org.apache.poi:poi-ooxml-full:5.2.3'
    implementation 'org.apache.poi:poi-ooxml-lite:5.2.3'
    implementation 'org.apache.xmlbeans:xmlbeans:5.1.1'

4.x的依赖

    implementation 'org.apache.poi:poi:4.1.2'
    implementation 'org.apache.poi:poi-ooxml:4.1.2'
    implementation 'org.apache.poi:ooxml-schemas:1.4'
    implementation 'org.apache.xmlbeans:xmlbeans:3.1.0'

我发现 ooxml-schemas:1.4 包含这些方法,但 poi-ooxml-full:5.2.3 不包含这些方法

那么有什么好的解决办法吗?

apache-poi
1个回答
1
投票

CTR.isSetRPr
应该进一步工作。我猜你的意思是
CTRPr.isSetRFonts
无法使用最新的
poi-ooxml-full-*
。对于所有
CTRPr
都相同 - set/isSet/get-methods - 过去的一个元素与现在的列表。

请参阅如何使用最新 Apache POI 5.2.2 的低级 CTRPr 和 CTPPr 类,了解更改原因的说明。

简而言之:微软已更改其 XSD,现在声明在一个运行属性中可能有多个字体设置和多个阴影设置。这就是为什么

XMLBeans
现在为
List
中这些元素的
Array
和/或
CTRPr
生成代码:现在的
CTRPr.getRFontsList
与过去的
CTRPr.getRFonts
,现在的
CTRPr.getShdList
与过去的
CTRPr.getShd
过去,...

所以如果需要检查

CTRPr
是否有字体设置,需要检查
CTRPr.getRFontsList
是否返回一个
java.util.List<CTFonts>
大于0的
size
。而要获取
CTFonts
,需要获取然后该列表中的第一项。与从
java.util.List<CTShd>
获得的
CTRPr.getShdList
相同。

对于以前使用的

STOnOff.TRUE
/
STOnOff.FALSE
/
STOnOff.ON
/
STOnOff.OFF
XMLBeans
现在生成直接使用
boolean
类型 (
true
/
false
) 的代码(如果只有两种可能的状态) 。因此,对于
CTOnOff.setVal
,现在可以直接使用
true
false
来代替
STOnOff.TRUE
STOnOff.FALSE
STOnOff.ON
STOnOff.OFF

不幸的是,

XMLBeans
自动生成的所有类的API文档都没有公开。因此,如果我们需要它 - 如果需要使用这些类,那么我们需要下载 ooxml-schemas-*-sources.jar(最高 Apache POI 4)或
poi-ooxml-full-*-sources.jar
(自 Apache POI 5 起)。然后解压缩并执行
javadoc
以获得
-subpackages org
。之后我们在目标目录中找到 API 文档。使用
overview-tree.html
开始阅读。
    

© www.soinside.com 2019 - 2024. All rights reserved.