ピボット テーブルでピボット フィールドをグループ化する

考えられる使用シナリオ

Microsoft Excel では、ピボット テーブルのピボット フィールドをグループ化できます。ピボット フィールドに関連するデータが大量にある場合、それらをセクションにグループ化すると便利なことがよくあります。 Aspose.Cells も、[PivotTable.setManualGroupField()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#setManualGroupField(com.aspose.cells.PivotField,%20com.aspose.cells.DateTime,%20com.aspose.cells.DateTime,%20java.util.ArrayList,%20int)) 方法。

ピボット テーブルでピボット フィールドをグループ化する

次のサンプル コードは、サンプル Excel ファイルを使用して最初のピボット フィールドでグループ化を実行します。[PivotTable.setManualGroupField()](https://reference.aspose.com/cells/java/com.aspose.cells/pivottable#setManualGroupField(com.aspose.cells.PivotField,%20com.aspose.cells.DateTime,%20com.aspose.cells.DateTime,%20java.util.ArrayList,%20int)) 方法。次に、ピボット テーブルのデータを更新して計算し、ワークブックを出力エクセルファイル.スクリーンショットは、サンプル Excel ファイルに対するサンプル コードの効果を示しています。スクリーンショットでわかるように、最初のピボット フィールドは月と四半期でグループ化されています。

todo:画像_代替_文章

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Load sample workbook
Workbook wb = new Workbook("sampleGroupPivotFieldsInPivotTable.xlsx");
//Access the second worksheet
Worksheet ws = wb.getWorksheets().get(1);
//Access the pivot table
PivotTable pt = ws.getPivotTables().get(0);
//Specify the start and end date time
DateTime dtStart = new DateTime(2008, 1, 1);//1-Jan-2018
DateTime dtEnd = new DateTime(2008, 9, 5); //5-Sep-2018
//Specify the group type list, we want to group by months and quarters
int[] groupTypeList = new int[2];
groupTypeList[0] = PivotGroupByType.MONTHS;
groupTypeList[1] = PivotGroupByType.QUARTERS;
//Apply the grouping on the pivot field
PivotField field = pt.getRowFields().get(0);
field.groupBy(dtStart, dtEnd, groupTypeList, 1, true);
//Refresh and calculate pivot table
pt.setRefreshDataFlag(true);
pt.refreshData();
pt.calculateData();
pt.setRefreshDataFlag(false);
//Save the output Excel file
wb.save("outputGroupPivotFieldsInPivotTable.xlsx");