データフィルタリング
データのオートフィルター
オートフィルターは、ワークシートからリストに表示する項目のみを選択する最も簡単な方法です。オートフィルター機能を使用すると、ユーザーは設定された基準に従ってリスト内のアイテムをフィルター処理できます。テキスト、数値、または日付に基づいてフィルタリングします。
Microsoft Excel のオートフィルター
Microsoft Excel でオートフィルター機能を有効にするには:
- ワークシートの見出し行をクリックします。
- からデータメニュー、選択フィルターその後オートフィルター.
ワークシートにオートフィルターを適用すると、列見出しの右側にフィルター スイッチ (黒い矢印) が表示されます。
- フィルタの矢印をクリックして、フィルタ オプションのリストを表示します。
オートフィルタ オプションの一部は次のとおりです。
オプション | 説明 |
---|---|
全て | リスト内のすべてのアイテムを 1 回表示します。 |
カスタム | 含む/含まないなどのフィルター基準をカスタマイズする |
色でフィルター | 塗りつぶし色に基づくフィルター |
日付フィルター | 日付のさまざまな基準に基づいて行をフィルター処理します |
数値フィルター | 比較、平均、上位 10 などの数値に対するさまざまなタイプのフィルター。 |
テキスト フィルター | で始まる、終わる、含むなどのさまざまなフィルター、 |
ブランクス/ノンブランクス | これらのフィルターは、Text Filter Blank を使用して実装できます。 |
ユーザーは、これらのオプションを使用して、Microsoft Excel でワークシート データを手動でフィルター処理します。
Aspose.Cells のオートフィルター
Aspose.Cells は、Excel ファイルを表すクラス Workbook を提供します。 Workbook クラスには、Excel ファイル内の各ワークシートへのアクセスを可能にする Worksheets コレクションが含まれています。
ワークシートは Worksheet クラスによって表されます。 Worksheet クラスは、ワークシートを管理するためのさまざまなプロパティとメソッドを提供します。オートフィルターを作成するには、Worksheet クラスの AutoFilter プロパティを使用します。 AutoFilter プロパティは、見出し行を構成するセルの範囲を指定するための Range プロパティを提供する AutoFilter クラスのオブジェクトです。見出し行であるセル範囲にオートフィルターが適用されます。
各ワークシートでは、1 つのフィルター範囲のみを指定できます。これは、Microsoft Excel によって制限されます。カスタム データ フィルタリングには、AutoFilter.Custom メソッドを使用します。
以下の例では、上記のセクションで Microsoft Excel を使用して作成したのと同じ AutoFilter を Aspose.Cells を使用して作成しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating AutoFilter by giving the cells range of the heading row | |
worksheet.AutoFilter.Range = "A1:B1"; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
さまざまな種類のフィルター
Aspose.Cells には、カラー フィルター、日付フィルター、数値フィルター、テキスト フィルター、空白フィルター、なし空白フィルターなど、さまざまな種類のフィルターを適用する複数のオプションが用意されています。
塗りつぶしの色
Aspose.Cells は、関数 AddFillColorFilter を提供して、セルの塗りつぶしの色のプロパティに基づいてデータをフィルター処理します。以下の例では、シートの最初の列に異なる塗りつぶし色を持つテンプレート ファイルを使用して、カラー フィルタリング機能をテストしています。サンプルファイルは以下のリンクからダウンロードできます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "ColouredCells.xlsx"); | |
// Instantiating a CellsColor object for foreground color | |
CellsColor clrForeground = workbook.CreateCellsColor(); | |
clrForeground.Color = Color.Red; | |
// Instantiating a CellsColor object for background color | |
CellsColor clrBackground = workbook.CreateCellsColor(); | |
clrBackground.Color = Color.White; | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call AddFillColorFilter function to apply the filter | |
worksheet.AutoFilter.AddFillColorFilter(0, BackgroundType.Solid, clrForeground, clrBackground); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredColouredCells.xlsx"); |
日にち
2018 年 1 月の日付を持つすべての行をフィルタリングするなど、さまざまなタイプの日付フィルターを実装できます。次のサンプル コードは、AddDateFilter 関数を使用したこのフィルターを示しています。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Date.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call AddDateFilter function to apply the filter | |
worksheet.AutoFilter.AddDateFilter(0, DateTimeGroupingType.Month, 2018, 1, 0, 0, 0, 0); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredDate.xlsx"); |
動的日付
年に関係なく 1 月の日付を持つすべてのセルのように、日付に基づく動的フィルターが必要になる場合があります。この場合、次のサンプル コードに示すように DynamicFilter 関数が使用されます。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Date.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call DynamicFilter function to apply the filter | |
worksheet.AutoFilter.DynamicFilter(0, DynamicFilterType.January); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredDynamicDate.xlsx"); |
番号
カスタム フィルターは、Aspose.Cells を使用して、特定の範囲内の数値を持つセルを選択するように適用できます。次の例は、 Custom() 関数を使用して数値をフィルタリングする方法を示しています。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Number.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call Custom function to apply the filter | |
worksheet.AutoFilter.Custom(0, FilterOperatorType.GreaterOrEqual, 5, true, FilterOperatorType.LessOrEqual, 10); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredNumber.xlsx"); |
文章
列にテキストが含まれており、特定のテキストを含むセルを選択する場合は、Filter() 関数を使用できます。次の例では、テンプレート ファイルに国のリストが含まれており、特定の国名を含む行が選択されます。次のコードは、テキストのフィルタリングを示しています。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Text.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call Filter function to apply the filter | |
worksheet.AutoFilter.Filter(0, "Angola"); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredText.xlsx"); |
ブランクス
列に空白のセルがほとんどないようなテキストが含まれており、空白のセルが存在する行のみを選択するためにフィルターが必要な場合は、以下に示すように MatchBlanks() 関数を使用できます。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call MatchBlanks function to apply the filter | |
worksheet.AutoFilter.MatchBlanks(0); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredBlank.xlsx"); |
非ブランク
テキストを含むセルをフィルター処理する場合は、以下に示すように MatchNonBlanks フィルター関数を使用します。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(sourceDir + "Blank.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Call MatchNonBlanks function to apply the filter | |
worksheet.AutoFilter.MatchNonBlanks(0); | |
// Call refresh function to update the worksheet | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "FilteredNonBlank.xlsx"); |
含むカスタム フィルター
Excel には、特定の文字列を含むフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、サンプル ファイル内の名前をフィルタリングすることによって以下に示されています。サンプルファイルを以下に示します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiating a Workbook object containing sample data | |
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating AutoFilter by giving the cells range | |
worksheet.AutoFilter.Range = "A1:A18"; | |
// Initialize filter for rows containing string "Ba" | |
worksheet.AutoFilter.Custom(0, FilterOperatorType.Contains, "Ba"); | |
//Refresh the filter to show/hide filtered rows | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save("outSourseSampleCountryNames.xlsx"); |
NotContains を使用したカスタム フィルター
Excel には、特定の文字列を含まないフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiating a Workbook object containing sample data | |
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating AutoFilter by giving the cells range | |
worksheet.AutoFilter.Range = "A1:A18"; | |
// Initialize filter for rows containing string "Ba" | |
worksheet.AutoFilter.Custom(0, FilterOperatorType.NotContains, "Be"); | |
//Refresh the filter to show/hide filtered rows | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save("outSourseSampleCountryNames.xlsx"); |
BeginsWith を使用したカスタム フィルター
Excel には、特定の文字列で始まるフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiating a Workbook object containing sample data | |
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating AutoFilter by giving the cells range | |
worksheet.AutoFilter.Range = "A1:A18"; | |
// Initialize filter for rows starting with string "Ba" | |
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "Ba"); | |
//Refresh the filter to show/hide filtered rows | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx"); |
EndsWith を使用したカスタム フィルター
Excel には、特定の文字列で終わるフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiating a Workbook object containing sample data | |
Workbook workbook = new Workbook(sourceDir + "sourseSampleCountryNames.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating AutoFilter by giving the cells range | |
worksheet.AutoFilter.Range = "A1:A18"; | |
// Initialize filter for rows end with string "ia" | |
worksheet.AutoFilter.Custom(0, FilterOperatorType.BeginsWith, "ia"); | |
//Refresh the filter to show/hide filtered rows | |
worksheet.AutoFilter.Refresh(); | |
// Saving the modified Excel file | |
workbook.Save(outputDir + "outSourseSampleCountryNames.xlsx"); |