Raggruppa i campi pivot nella tabella pivot
Possibili Scenari di Utilizzo
Microsoft Excel consente di raggruppare i campi pivot della tabella pivot. Quando c’è una grande quantità di dati relativi a un campo pivot, è spesso utile raggrupparli in sezioni. Aspose.Cells fornisce anche questa funzionalità utilizzando il metodo PivotTable.SetManualGroupField().
Raggruppa i campi pivot nella tabella pivot
Il codice di esempio seguente carica il file Excel di esempio e esegue il raggruppamento sul primo campo pivot utilizzando il metodo PivotTable.SetManualGroupField(). Aggiorna quindi e calcola i dati della tabella pivot e salva il foglio di calcolo come file Excel di output. Lo screenshot mostra l’effetto del codice di esempio sul file Excel di esempio. Come si può vedere dallo screenshot, il primo campo pivot è ora raggruppato per mesi e trimestri.
Codice di Esempio
// 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"); |