Aggiungere Filtro nella Tabella Pivot
Possibili Scenari di Utilizzo
Quando si crea una tabella pivot con dati noti e si desidera filtrare la tabella pivot, è necessario imparare e utilizzare il filtro. Può aiutarti a filtrare efficacemente i dati desiderati. Utilizzando l’API Aspose.Cells per Python via .NET, è possibile aggiungere un filtro sui valori del campo nelle Tabelle Pivot.
Aggiungere un Filtro nella Tabella Pivot utilizzando la libreria Excel Aspose.Cells per Python
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
from aspose.cells import ConsolidationFunction, Workbook | |
from aspose.cells.pivot import PivotFieldType, PivotFilterType | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
# Setting the value to the cells | |
cell = cells.get("A1") | |
cell.put_value("Fruit") | |
cell = cells.get("B1") | |
cell.put_value("Count") | |
cell = cells.get("A2") | |
cell.put_value("Apple") | |
cell = cells.get("A3") | |
cell.put_value("Mango") | |
cell = cells.get("A4") | |
cell.put_value("Blackberry") | |
cell = cells.get("A5") | |
cell.put_value("Cherry") | |
cell = cells.get("A6") | |
cell.put_value("Guava") | |
cell = cells.get("A7") | |
cell.put_value("Carambola") | |
cell = cells.get("A8") | |
cell.put_value("Banana") | |
cell = cells.get("B2") | |
cell.put_value(5) | |
cell = cells.get("B3") | |
cell.put_value(3) | |
cell = cells.get("B4") | |
cell.put_value(6) | |
cell = cells.get("B5") | |
cell.put_value(4) | |
cell = cells.get("B6") | |
cell.put_value(5) | |
cell = cells.get("B7") | |
cell.put_value(2) | |
cell = cells.get("B8") | |
cell.put_value(20) | |
# Adding a PivotTable to the worksheet | |
i = ws.pivot_tables.add("=A1:B8", "D10", "PivotTable1") | |
# Accessing the instance of the newly added PivotTable | |
pivotTable = ws.pivot_tables[i] | |
pivotTable.add_field_to_area(PivotFieldType.ROW, 0) | |
pivotTable.add_field_to_area(PivotFieldType.DATA, "Count") | |
pivotTable.data_fields[0].function = ConsolidationFunction.SUM | |
field = pivotTable.row_fields[0] | |
field.is_auto_sort = True | |
field.is_ascend_sort = False | |
field.auto_sort_field = 0 | |
# Add top10 filter | |
index = pivotTable.pivot_filters.add(0, PivotFilterType.COUNT) | |
filter = pivotTable.pivot_filters[index] | |
filter.auto_filter.filter_top10(0, True, False, 5) | |
pivotTable.refresh_data() | |
pivotTable.calculate_data() | |
workbook.save("out.xlsx") |