透视筛选器
Contents
[
Hide
]
可能的使用场景
当你使用已知数据创建数据透视表并希望筛选时,你需要学习并使用筛选功能。它可以帮助你有效筛选出所需的数据。通过使用Aspose.Cells API,你可以在数据透视表中添加和清除字段值上的筛选。
在Excel中为数据透视表添加筛选
在Excel中为数据透视表添加筛选,按照以下步骤操作:
- 选择要清除筛选的数据透视表。
- 点击你想要添加筛选的下拉箭头。
- 从下拉菜单中选择“前十名”。
- 设置显示模式和筛选数量。
在数据透视表中添加筛选
请参阅以下示例代码。它设置数据并基于它创建数据透视表。然后在数据透视表的行字段上添加筛选器。最后,以[filterout.xlsx]格式保存工作簿。执行示例代码后,工作表中将添加一个带有Top10筛选器的数据透视表。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |
在Excel中清除数据透视表的筛选
在 Excel 中清除数据透视表中的筛选,按照以下步骤操作:
- 选择要清除筛选的数据透视表。
- 单击数据透视表中要清除筛选的下拉箭头。
- 从下拉菜单中选择“清除筛选”。
- 如果您要清除数据透视表中的所有筛选,还可以在 Excel 的“数据透视表分析”选项卡上单击“清除筛选”按钮。
清除数据透视表的筛选
使用 Aspose.Cells 在数据透视表中清除筛选。请参阅以下示例代码。
- 设置数据并创建基于该数据的数据透视表。
- 在数据透视表的行字段上添加筛选。
- 以 output XLSX 格式保存工作簿。执行示例代码后,将在工作表中添加带有 top10 筛选的数据透视表。
- 清除特定数据透视字段上的筛选。执行清除筛选的代码后,将清除特定数据透视字段上的筛选。请检查 output XLSX。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |