在透视表中对透视字段进行分组
Contents
[
Hide
]
可能的使用场景
Microsoft Excel允许您对透视表的透视字段进行分组。当与透视字段相关的数据量很大时,通常将它们分组成部分很有用。Aspose.Cells也提供使用PivotTable.SetManualGroupField()方法的此功能。
在透视表中对透视字段进行分组
以下示例代码加载示例Excel文件,并使用PivotTable.SetManualGroupField()方法对第一个透视字段执行分组。然后刷新和计算透视表的数据,并将工作簿保存为输出Excel文件。屏幕截图显示了示例代码对示例Excel文件的效果。如屏幕截图所示,第一个透视字段现在按月份和季度分组。
示例代码
This file contains 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"); |