ピボットフィルタ
可能な使用シナリオ
既知のデータを使ってピボットテーブルを作成し、フィルタリングしたい場合は、フィルタを学び、使う必要があります。これにより、目的のデータを効果的にフィルターできます。Aspose.Cells APIを使用すると、フィールド値にフィルタを追加およびクリアできます。
Excelでピボットテーブルにフィルタを追加する方法
Excelのピボットテーブルにフィルタを追加するには、次の手順に従います:
- クリアしたいPivotTableを選択します。
- ピボットテーブルに追加したいフィルタのドロップダウン矢印をクリックします。
- ドロップダウンメニューから「トップ10」を選択します。
- 表示モードとフィルタの数を設定します。
ピボットテーブルにフィルタを追加
次のサンプルコードを参照してください。データを設定し、それに基づいてPivotTableを作成します。次に、ピボットテーブルの行フィールドにフィルタを追加します。最後に、output XLSX形式でブックを保存します。サンプルコードを実行した後は、ワークシートにトップ10フィルタが追加されたピボットテーブルが表示されます。
サンプルコード
//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でピボットテーブルのフィルタをクリアするには、以下の手順に従います:
- クリアしたいPivotTableを選択します。
- ピボットテーブルでクリアしたいフィルタのドロップダウン矢印をクリックします。
- ドロップダウンメニューから「フィルタをクリア」を選択します。
- ピボットテーブルからすべてのフィルタをクリアしたい場合は、ExcelのリボンのPivotTable Analyzeタブで「フィルタをクリア」ボタンをクリックすることもできます。
ピボットテーブルのフィルタをクリア
Aspose.Cellsを使用したピボットテーブルでのフィルタのクリア。次のサンプルコードを参照してください。
- データを設定し、それに基づいてPivotTableを作成します。
- ピボットテーブルの行フィールドにフィルタを追加します。
- output XLSX形式でブックを保存します。サンプルコードを実行した後は、ワークシートにトップ10フィルタが追加されたピボットテーブルが表示されます。
- 特定のピボットフィールドのフィルタをクリアします。フィルタをクリアするコードを実行した後、特定のピボットフィールドのフィルタがクリアされます。output XLSXをご確認ください。
サンプルコード
//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"); |