使用Apache POI将列标签插入数据透视表?

时间:2014-10-20 07:07:16

标签: java excel pivot apache-poi

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

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

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

img

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

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

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

感谢。

2 个答案:

答案 0 :(得分:10)

以下方法(稍微修改过的XSSFPivotTable.addRowLabel版本)添加了一个" normal"枢轴列标签:

With Range("InsuredName")
    .Value = "=INDEX(Data,RetrievalRow,InsuredNameIndex)"
    .Value = .Value
End With

答案 1 :(得分:0)

从版本3.12 of POI开始,它就像一个魅力(也有自己的列标签):

//insert column
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 2, "Central");
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 3, "East");
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 4, "West");
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 5, "Grand Total");