You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache POI 5.2.3无法访问CTSkip类的问题求助

Apache POI 5.2.3 设置分类轴刻度间隔时CTSkip类找不到的解决办法

在使用Apache POI 5.2.3设置图表分类轴的刻度标签和标记间隔时,调用以下代码出现编译错误:

chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickLblSkip().setVal(2); // label only every second mark
chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickMarkSkip().setVal(2); // show only every second mark

错误信息:

error: cannot access CTSkip
class file for org.openxmlformats.schemas.drawingml.x2006.chart.CTSkip not found

同命名空间下的CTTitle、CTNumFormat等类可正常访问,仅CTSkip无法找到。

可行解决办法

  • 切换完整OOXML依赖包
    Apache POI的基础poi-ooxml包只包含核心OOXML类,CTSkip这类扩展类在poi-ooxml-full包中。替换依赖为完整包即可解决类缺失问题:
    Maven依赖示例:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-full</artifactId>
        <version>5.2.3</version>
    </dependency>
    

    Gradle依赖示例:

    implementation 'org.apache.poi:poi-ooxml-full:5.2.3'
    
  • 直接操作XML DOM节点(无需更换依赖)
    如果无法调整依赖包,可以绕过生成的CTSkip类,直接构造XML节点实现功能:

    // 获取分类轴XML元素
    org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx catAx = chart.getCTChart().getPlotArea().getCatAxArray(0);
    
    // 设置tickLblSkip
    if (catAx.getTickLblSkip() == null) {
        org.w3c.dom.Element tickLblSkip = catAx.getDomNode().getOwnerDocument().createElementNS("http://schemas.openxmlformats.org/drawingml/2006/chart", "c:tickLblSkip");
        org.w3c.dom.Element val = catAx.getDomNode().getOwnerDocument().createElementNS("http://schemas.openxmlformats.org/drawingml/2006/chart", "c:val");
        val.setTextContent("2");
        tickLblSkip.appendChild(val);
        catAx.getDomNode().appendChild(tickLblSkip);
    }
    
    // 设置tickMarkSkip
    if (catAx.getTickMarkSkip() == null) {
        org.w3c.dom.Element tickMarkSkip = catAx.getDomNode().getOwnerDocument().createElementNS("http://schemas.openxmlformats.org/drawingml/2006/chart", "c:tickMarkSkip");
        org.w3c.dom.Element val = catAx.getDomNode().getOwnerDocument().createElementNS("http://schemas.openxmlformats.org/drawingml/2006/chart", "c:val");
        val.setTextContent("2");
        tickMarkSkip.appendChild(val);
        catAx.getDomNode().appendChild(tickMarkSkip);
    }
    
  • 统一依赖版本
    确保所有POI相关依赖(如poi、poi-ooxml、poi-scratchpad等)的版本均为5.2.3,避免版本冲突导致类加载异常。

内容的提问来源于stack exchange,提问作者denver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:01:12