Filtro pivot
Possibili Scenari di Utilizzo
Quando crei una tabella pivot con dati noti e desideri filtrarla, devi imparare e usare i filtri. Può aiutarti a filtrare efficacemente i dati desiderati. Usando l’API Aspose.Cells, puoi aggiungere e cancellare filtri sui valori dei campi nelle tabelle pivot.
Aggiungi filtro in Pivot Table in Excel
Per aggiungere un filtro in Pivot Table in Excel, segui questi passaggi:
- Seleziona la tabella pivot da cui desideri eliminare il filtro.
- Clicca sulla freccia a discesa per il filtro che desideri aggiungere in pivot table.
- Seleziona “Top 10” dal menu a discesa.
- Imposta la modalità di visualizzazione e il numero di filtri.
Aggiungi filtro in Pivot Table
Si prega di consultare il codice di esempio seguente. Imposta i dati e crea una tabella pivot basata su di essi. Quindi aggiungi un filtro sul campo di riga della tabella pivot. Infine, salvare il libro di lavoro nel formato output XLSX. Dopo aver eseguito il codice di esempio, viene aggiunta una tabella pivot con filtro top10 al foglio di lavoro.
Codice di Esempio
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Aspose.Cells.Cells cells = ws.Cells; | |
//Setting the value to the cells | |
Aspose.Cells.Cell cell = cells["A1"]; | |
cell.PutValue("Fruit"); | |
cell = cells["B1"]; | |
cell.PutValue("Count"); | |
cell = cells["A2"]; | |
cell.PutValue("Apple"); | |
cell = cells["A3"]; | |
cell.PutValue("Mango"); | |
cell = cells["A4"]; | |
cell.PutValue("Blackberry"); | |
cell = cells["A5"]; | |
cell.PutValue("Cherry"); | |
cell = cells["A6"]; | |
cell.PutValue("Guava"); | |
cell = cells["A7"]; | |
cell.PutValue("Carambola"); | |
cell = cells["A8"]; | |
cell.PutValue("Banana"); | |
cell = cells["B2"]; | |
cell.PutValue(5); | |
cell = cells["B3"]; | |
cell.PutValue(3); | |
cell = cells["B4"]; | |
cell.PutValue(6); | |
cell = cells["B5"]; | |
cell.PutValue(4); | |
cell = cells["B6"]; | |
cell.PutValue(5); | |
cell = cells["B7"]; | |
cell.PutValue(2); | |
cell = cells["B8"]; | |
cell.PutValue(20); | |
//Adding a PivotTable to the worksheet | |
int i = ws.PivotTables.Add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
PivotTable pivotTable = ws.PivotTables[i]; | |
pivotTable.AddFieldToArea(PivotFieldType.Row, 0); | |
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, "Count"); | |
pivotTable.DataFields[0].Function = ConsolidationFunction.Sum; | |
PivotField field = pivotTable.RowFields[0]; | |
field.IsAutoSort = true; | |
field.IsAscendSort = false; | |
field.AutoSortField = 0; | |
//Add top10 filter | |
PivotField filterField = pivotTable.RowFields[0]; | |
filterField.FilterTop10(0, PivotFilterType.Count, false, 5); | |
pivotTable.RefreshData(); | |
pivotTable.CalculateData(); | |
workbook.Save("filterout.xlsx"); |
Cancella filtro in Pivot Table in Excel
Elimina filtro nella tabella pivot in Excel, segui questi passaggi:
- Seleziona la tabella pivot da cui desideri eliminare il filtro.
- Fai clic sulla freccia a discesa per il filtro che desideri eliminare nella tabella pivot.
- Seleziona “Elimina filtro” dal menu a discesa.
- Se desideri eliminare tutti i filtri dalla tabella pivot, puoi anche fare clic sul pulsante “Elimina filtri” nella scheda Analizza tabella pivot nel nastro di Excel.
Cancella filtro in Pivot Table
Cancella il filtro nella tabella pivot utilizzando Aspose.Cells. Si prega di vedere il seguente codice di esempio.
- Impostare i dati e creare una tabella pivot basata su di essi.
- Aggiungere un filtro sul campo di riga della tabella pivot.
- Salvare il workbook nel formato output XLSX. Dopo aver eseguito il codice di esempio, una tabella pivot con filtro top10 viene aggiunta al foglio di lavoro.
- Cancella il filtro su un campo pivot specifico. Dopo aver eseguito il codice per cancellare il filtro, il filtro sul campo pivot specifico verrà cancellato. Si prega di controllare il output XLSX.
Codice di Esempio
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Aspose.Cells.Cells cells = ws.Cells; | |
//Setting the value to the cells | |
Aspose.Cells.Cell cell = cells["A1"]; | |
cell.PutValue("Fruit"); | |
cell = cells["B1"]; | |
cell.PutValue("Count"); | |
cell = cells["A2"]; | |
cell.PutValue("Apple"); | |
cell = cells["A3"]; | |
cell.PutValue("Mango"); | |
cell = cells["A4"]; | |
cell.PutValue("Blackberry"); | |
cell = cells["A5"]; | |
cell.PutValue("Cherry"); | |
cell = cells["A6"]; | |
cell.PutValue("Guava"); | |
cell = cells["A7"]; | |
cell.PutValue("Carambola"); | |
cell = cells["A8"]; | |
cell.PutValue("Banana"); | |
cell = cells["B2"]; | |
cell.PutValue(5); | |
cell = cells["B3"]; | |
cell.PutValue(3); | |
cell = cells["B4"]; | |
cell.PutValue(6); | |
cell = cells["B5"]; | |
cell.PutValue(4); | |
cell = cells["B6"]; | |
cell.PutValue(5); | |
cell = cells["B7"]; | |
cell.PutValue(2); | |
cell = cells["B8"]; | |
cell.PutValue(20); | |
//Adding a PivotTable to the worksheet | |
int i = ws.PivotTables.Add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
PivotTable pivotTable = ws.PivotTables[i]; | |
pivotTable.AddFieldToArea(PivotFieldType.Row, 0); | |
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, "Count"); | |
pivotTable.DataFields[0].Function = ConsolidationFunction.Sum; | |
PivotField field = pivotTable.RowFields[0]; | |
field.IsAutoSort = true; | |
field.IsAscendSort = false; | |
field.AutoSortField = 0; | |
//Add top10 filter | |
PivotField filterField = pivotTable.RowFields[0]; | |
filterField.FilterTop10(0, PivotFilterType.Count, false, 5); | |
pivotTable.RefreshData(); | |
pivotTable.CalculateData(); | |
workbook.Save("out_add.xlsx"); | |
//Clear PivotFilter from the specific PivotField | |
pivotTable.PivotFilters.ClearFilter(field.BaseIndex); | |
pivotTable.RefreshData(); | |
pivotTable.CalculateData(); | |
workbook.Save("out_delete.xlsx"); |