Raggruppa i campi pivot nella tabella pivot
Possibili Scenari di Utilizzo
Microsoft Excel consente di raggruppare i campi pivot della tabella pivot. Quando ci sono molte informazioni relative a un campo pivot, è spesso utile raggrupparle in sezioni. Anche Aspose.Cells fornisce questa funzionalità utilizzando il metodo PivotTable.setManualGroupField().
Raggruppa i campi pivot nella tabella pivot
Il seguente codice di esempio carica il file di 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 documento di lavoro come il file di Excel di output. Lo screenshot mostra l’effetto del codice di esempio sul file di Excel di esempio. Come si può vedere nello 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-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"); |