数据透视表中的分组数据透视字段

可能的使用场景

Microsoft Excel 允许您对数据透视表的数据透视字段进行分组。当有大量数据与数据透视字段相关时,将它们分组到部分通常很有用。 Aspose.Cells 也使用数据透视表.SetManualGroupField()方法。

数据透视表中的分组数据透视字段

下面的示例代码加载示例 Excel 文件并使用数据透视表.SetManualGroupField()方法。然后它刷新并计算数据透视表的数据并将工作簿另存为输出Excel文件.屏幕截图显示了示例代码对示例 Excel 文件的影响。正如您在屏幕截图中看到的,第一个数据透视字段现在按月和季度分组。

待办事项:图片_替代_文本

示例代码

// 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");