POI将列标签插入到数据透视表中

POI将列标签插入到数据透视表中

本文介绍了使用Apache POI将列标签插入到数据透视表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Apache POI 3.11创建了一个数据透视表。像这样:

  FileInputStream file = new FileInputStream(new File(path + fname)); 

XSSFWorkbook工作簿=新的XSSFWorkbook(文件);

XSSFSheet sheet = workbook.getSheetAt(0);
//枢纽数据区域
AreaReference a = new AreaReference(A1:J4);

CellReference b = new CellReference(N5);
XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);

//插入行
pivotTable.addRowLabel(3);
pivotTable.addRowLabel(6);

// insert column
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT,5);

// export
FileOutputStream output_file =
new FileOutputStream(new File(path +POI_XLS_Pivot_Example.xlsx));
workbook.write(output_file); //将excel文件写入输出流
output_file.close(); //关闭文件

生成报告后,它正确显示该行。但是它没有显示列标签:





我想在我的数据透视表中显示列标签:





有没有人知道这个问题的解决方案?



谢谢。

解决方案

以下方法(XSSFPivotTable.addRowLabel的稍微修改版本)添加了一个正常标签:

  public static void addColLabel(XSSFPivotTable pivotTable,int columnIndex){
AreaReference pivotArea = new AreaReference(pivotTable。 getPivotCacheDefinition()。getCTPivotCacheDefinition()
.getCacheSource()。getWorksheetSource()。getRef());
int lastRowIndex = pivotArea.getLastCell()。getRow() - pivotArea.getFirstCell()。getRow();
int lastColIndex = pivotArea.getLastCell()。getCol() - pivotArea.getFirstCell()。getCol();

if(columnIndex> lastColIndex){
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); (int i = 0; i items.addNewItem()。setT(STItemType.DEFAULT);

}
items.setCount(items.sizeOfItemArray());
pivotFields.setPivotFieldArray(columnIndex,pivotField);

CTColFields rowFields;
if(pivotTable.getCTPivotTableDefinition()。getColFields()!= null){
rowFields = pivotTable.getCTPivotTableDefinition()。getColFields();
} else {
rowFields = pivotTable.getCTPivotTableDefinition()。addNewColFields();
}

rowFields.addNewField()。setX(columnIndex);
rowFields.setCount(rowFields.sizeOfFieldArray());
}


I've created a pivot table using Apache POI 3.11. like this:

FileInputStream file = new FileInputStream(new File(path+fname));

XSSFWorkbook workbook = new  XSSFWorkbook(file);

XSSFSheet sheet = workbook.getSheetAt(0);
//area of pivot data
AreaReference a=new AreaReference("A1:J4");

CellReference b=new CellReference("N5");
XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);

//insert row
pivotTable.addRowLabel(3);
pivotTable.addRowLabel(6);

//insert column
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 5);

//export
FileOutputStream output_file =
   new FileOutputStream(new File(path+"POI_XLS_Pivot_Example.xlsx"));
workbook.write(output_file);//write excel document to output stream
output_file.close(); //close the file

After I generated the report, it shows the row correctly. But it doesn't show a column label:

I want to display the column label in my pivot table like this:

img http://www.pivot-table.com/wp-content/uploads/2010/12/calculateditem04.png

Does anyone know the solution for this problem?

Thanks.

解决方案

The following method (a slightly modified version of XSSFPivotTable.addRowLabel) adds a "normal" pivot column label:

public static void addColLabel(XSSFPivotTable pivotTable, int columnIndex) {
    AreaReference pivotArea = new AreaReference(pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition()
            .getCacheSource().getWorksheetSource().getRef());
    int lastRowIndex = pivotArea.getLastCell().getRow() - pivotArea.getFirstCell().getRow();
    int lastColIndex = pivotArea.getLastCell().getCol() - pivotArea.getFirstCell().getCol();

    if (columnIndex > lastColIndex) {
        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 <= lastRowIndex; i++) {
        items.addNewItem().setT(STItemType.DEFAULT);
    }
    items.setCount(items.sizeOfItemArray());
    pivotFields.setPivotFieldArray(columnIndex, pivotField);

    CTColFields rowFields;
    if (pivotTable.getCTPivotTableDefinition().getColFields() != null) {
        rowFields = pivotTable.getCTPivotTableDefinition().getColFields();
    } else {
        rowFields = pivotTable.getCTPivotTableDefinition().addNewColFields();
    }

    rowFields.addNewField().setX(columnIndex);
    rowFields.setCount(rowFields.sizeOfFieldArray());
}

这篇关于使用Apache POI将列标签插入到数据透视表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:48