Apache POI添加列标签

Ant*_*ten 4 java apache excel pivot-table apache-poi

如何使用Apache POI 3.12添加Colum Label.

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
Run Code Online (Sandbox Code Playgroud)

枢轴详细信息:行标签 - 国家/地区.(工作正常)Colum标签 - 团队(无法使用POI添加此项)报告过滤器 - 播放器状态(正常工作)和值 - 名称计数(工作正常)

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

小智 7

我重写了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());
}
Run Code Online (Sandbox Code Playgroud)