Pivot Filter
Mögliche Verwendungsszenarien
Wenn Sie ein PivotTable mit bekannten Daten erstellen und das PivotTable filtern möchten, müssen Sie Filter lernen und verwenden. Es kann Ihnen helfen, die Daten, die Sie effektiv filtern möchten, auszuwählen. Mit der Aspose.Cells Java-API können Sie Filter auf Feldwerte in Pivot-Tabellen hinzufügen.
Filter in Pivot-Tabelle in Excel hinzufügen
Filter in Pivot-Tabelle in Excel hinzufügen, folgen Sie diesen Schritten:
- Wählen Sie die Pivot-Tabelle aus, aus der Sie den Filter löschen möchten.
- Klicken Sie auf den Dropdown-Pfeil für den Filter, den Sie in der Pivot-Tabelle hinzufügen möchten.
- Wählen Sie “Top 10” aus dem Dropdown-Menü.
- Legen Sie den Anzeigemodus und die Filteranzahl fest.
Filter in Pivot-Tabelle hinzufügen
Bitte beachten Sie den folgenden Beispielcode. Es setzt die Daten und erstellt ein PivotTable basierend darauf. Fügt dann ein Filter auf das Zeilenfeld der Pivottabelle hinzu. Schließlich speichert es die Arbeitsmappe im output XLSX-Format. Nach Ausführen des Beispielcodes wird ein PivotTable mit Top-10-Filter zum Arbeitsblatt hinzugefügt.
Beispielcode
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.getWorksheets().get(0); | |
Cells cells = ws.getCells(); | |
//Setting the value to the cells | |
Cell cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("A6"); | |
cell.putValue("Guava"); | |
cell = cells.get("A7"); | |
cell.putValue("Carambola"); | |
cell = cells.get("A8"); | |
cell.putValue("Banana"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("B6"); | |
cell.putValue(5); | |
cell = cells.get("B7"); | |
cell.putValue(2); | |
cell = cells.get("B8"); | |
cell.putValue(20); | |
//Adding a PivotTable to the worksheet | |
int i = ws.getPivotTables().add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
PivotTable pivotTable = ws.getPivotTables().get(i); | |
pivotTable.addFieldToArea(PivotFieldType.ROW, 0); | |
pivotTable.addFieldToArea(PivotFieldType.DATA, "Count"); | |
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.SUM); | |
PivotField field = pivotTable.getRowFields().get(0); | |
field.setAutoSort(true); | |
field.setAscendSort(false); | |
field.setAutoSortField(0); | |
//Add top10 filter | |
PivotField filterField = pivotTable.getRowFields().get(0); | |
filterField.filterTop10(0, PivotFilterType.COUNT, false, 5); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
workbook.save("out.xlsx"); |
Filter in Pivot-Tabelle in Excel löschen
Filter in Pivot-Tabelle in Excel löschen, befolgen Sie diese Schritte:
- Wählen Sie die Pivot-Tabelle aus, aus der Sie den Filter löschen möchten.
- Klicken Sie auf den Dropdown-Pfeil für den Filter, den Sie in der Pivot-Tabelle löschen möchten.
- Wählen Sie “Filter löschen” aus dem Dropdown-Menü aus.
- Wenn Sie alle Filter aus der Pivot-Tabelle löschen möchten, können Sie auch auf die Schaltfläche “Filter löschen” im PivotTable-Analyse-Tab im Menüband in Excel klicken.
Filter in Pivot-Tabelle löschen
Bitte beachten Sie den folgenden Beispielcode. Er setzt die Daten und erstellt eine Pivot-Tabelle basierend darauf. Fügen Sie dann einen Filter auf das Zeilenfeld der Pivot-Tabelle hinzu. Schließlich speichert er die Arbeitsmappe im Format Ausgabexlsx. Nach Ausführung des Beispielscodes wird ein Pivot-Table mit Top10-Filter zum Arbeitsblatt hinzugefügt. Nach Hinzufügen eines Filters, wenn wir unverarbeitete Daten benötigen, können wir den Filter auf einem spezifischen Pivotfeld löschen. Nach Ausführung des Codes zum Löschen des Filters wird der Filter auf dem spezifischen Pivot-Feld gelöscht. Bitte überprüfen Sie das Ausgabexlsx.
Beispielcode
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.getWorksheets().get(0); | |
Cells cells = ws.getCells(); | |
//Setting the value to the cells | |
Cell cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("A6"); | |
cell.putValue("Guava"); | |
cell = cells.get("A7"); | |
cell.putValue("Carambola"); | |
cell = cells.get("A8"); | |
cell.putValue("Banana"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("B6"); | |
cell.putValue(5); | |
cell = cells.get("B7"); | |
cell.putValue(2); | |
cell = cells.get("B8"); | |
cell.putValue(20); | |
//Adding a PivotTable to the worksheet | |
int i = ws.getPivotTables().add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
PivotTable pivotTable = ws.getPivotTables().get(i); | |
pivotTable.addFieldToArea(PivotFieldType.ROW, 0); | |
pivotTable.addFieldToArea(PivotFieldType.DATA, "Count"); | |
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.SUM); | |
PivotField field = pivotTable.getRowFields().get(0); | |
field.setAutoSort(true); | |
field.setAscendSort(false); | |
field.setAutoSortField(0); | |
//Add top10 filter | |
PivotField filterField = pivotTable.getRowFields().get(0); | |
filterField.filterTop10(0, PivotFilterType.COUNT, false, 5); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
workbook.save("out_add.xlsx"); | |
//Clear PivotFilter from the specific PivotField | |
pivotTable.getPivotFilters().clearFilter(field.getBaseIndex()); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
workbook.save("out_delete.xlsx"); |