2014-10-20 97 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

相關問題