Apache POI 添加列标签

Apache POI Add Column Label

如何使用 Apache POI 3.12 添加列标签。

Name Team Country Player Status

a   fcb z   active

b   rm  z   injured

c   fcb z   active

d   rm  z   injured

e   am  z   banned

f   rcb z   banned

g   rm  y   injured

h   am  y   active

i   am  y   active

数据透视详细信息:行标签 - 国家/地区。 (工作正常) 列标签 - 团队(无法使用 POI 添加) 报告过滤器 - 玩家状态(工作正常) & 值 - 名称计数(工作正常)

每当我使用 addColumLabel() 函数时,使用的列都会添加到值中!我应该使用 addDataColumn() 函数吗?如果是,应该如何使用?

我重写了 addDataColumn 方法如下,它可以正确添加 columnLabels。

public static void addColumLabels(XSSFPivotTable pivotTable, int columnIndex) {
    AreaReference pivotArea = getPivotArea(pivotTable);
    int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

    if (columnIndex > lastColIndex && columnIndex < 0) {
        throw new IndexOutOfBoundsException();
    }

    CTPivotFields pivotFields = pivotTable.getCTPivotTableDefinition().getPivotFields();

    CTPivotField pivotField = CTPivotField.Factory.newInstance();
    CTItems items = pivotField.addNewItems();

    pivotField.setAxis(STAxis.AXIS_COL);
    pivotField.setShowAll(false);
    for (int i = 0; i <= lastColIndex; i++) {
        items.addNewItem().setT(STItemType.DEFAULT);
    }
    items.setCount(items.sizeOfItemArray());
    pivotFields.setPivotFieldArray(columnIndex, pivotField);

    // colfield should be added for the second one.
    CTColFields colFields;
    if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
        colFields = pivotTable.getCTPivotTableDefinition().getColFields();
    } else {
        colFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
    }
    colFields.addNewField().setX(columnIndex);
    colFields.setCount(colFields.sizeOfFieldArray());
}