2014-10-20 99 views
6

我已经使用Apache POI 3.11创建了一个数据透视表。像这样:使用Apache POI将列标签插入数据透视表中?

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

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

谢谢。

+0

在这一刻我有完全一样的问题!那么,因为它是一个测试版本,现在可能不可能......可惜的是,没有方法'addColumnLabel(int)'这会添加没有数据合并功能的列标签......我搜索了一些示例代码,并发现这一点:https://code.google.com/p/web-design-r/source/browse/trunk/zpoiex-r/src/org/zkoss/zpoiex/ss/usermodel/helpers/PivotTableHelper。 java?r = 15 createPivotTable方法在处理一些'CTPivotField'的时候看起来很有趣。不知道,如果有通过这些领域的方式?! – bobbel 2014-10-29 17:07:36

+0

你是否得到这个工作?创建addColumnLabel如下面的答案仍然无法正常工作..感谢 – labheshr 2016-05-27 16:48:16

回答

9

下面的方法(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); 
    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()); 
} 
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"); 
+1

什么POI称为“列标签”在Excel中显示为“值”。这个方法应该被称为addValue – Gabriel 2016-04-22 16:22:54

+0

这个解决方案没有理由,因为'pivotTable.addColumnLabe'的第二个参数是** source **的'columnIndex'。 – 2016-11-07 11:02:34

相关问题