ピボット テーブルでピボット フィールドをグループ化する
Contents
[
Hide
]
考えられる使用シナリオ
Microsoft Excel では、ピボット テーブルのピボット フィールドをグループ化できます。ピボット フィールドに関連するデータが大量にある場合、それらをセクションにグループ化すると便利なことがよくあります。 Aspose.Cells も、PivotTable.SetManualGroupField()方法。
ピボット テーブルでピボット フィールドをグループ化する
次のサンプル コードは、サンプル Excel ファイルを使用して最初のピボット フィールドでグループ化を実行します。PivotTable.SetManualGroupField()方法。次に、ピボット テーブルのデータを更新して計算し、ワークブックを次のように保存します。出力エクセルファイル.スクリーンショットは、サンプル Excel ファイルに対するサンプル コードの効果を示しています。スクリーンショットでわかるように、最初のピボット フィールドは月と四半期でグループ化されています。
サンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load sample workbook | |
Workbook wb = new Workbook("sampleGroupPivotFieldsInPivotTable.xlsx"); | |
//Access the second worksheet | |
Worksheet ws = wb.Worksheets[1]; | |
//Access the pivot table | |
PivotTable pt = ws.PivotTables[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 | |
PivotGroupByType[] groupTypeList = new PivotGroupByType[2]; | |
groupTypeList[0] = PivotGroupByType.Months; | |
groupTypeList[1] = PivotGroupByType.Quarters; | |
//Apply the grouping on the pivot field | |
PivotField field = pt.RowFields[0]; | |
field.GroupBy(dtStart, dtEnd, groupTypeList, 1, true); | |
//Refresh and calculate pivot table | |
pt.RefreshDataFlag = true; | |
pt.RefreshData(); | |
pt.CalculateData(); | |
pt.RefreshDataFlag = false; | |
//Save the output Excel file | |
wb.Save("outputGroupPivotFieldsInPivotTable2.xlsx"); |