データフィルタリング

データのオートフィルター

オートフィルターは、ワークシートからリストに表示する項目のみを選択する最も簡単な方法です。オートフィルター機能を使用すると、ユーザーは設定された基準に従ってリスト内のアイテムをフィルター処理できます。テキスト、数値、または日付に基づいてフィルタリングします。

Microsoft Excel のオートフィルター

Microsoft Excel でオートフィルター機能を有効にするには:

  1. ワークシートの見出し行をクリックします。
  2. からデータメニュー、選択フィルターその後オートフィルター.

ワークシートにオートフィルターを適用すると、列見出しの右側にフィルター スイッチ (黒い矢印) が表示されます。

  1. フィルタの矢印をクリックして、フィルタ オプションのリストを表示します。

オートフィルタ オプションの一部は次のとおりです。

オプション 説明
全て リスト内のすべてのアイテムを 1 回表示します。
カスタム 含む/含まないなどのフィルター基準をカスタマイズする
色でフィルター 塗りつぶし色に基づくフィルター
日付フィルター 日付のさまざまな基準に基づいて行をフィルター処理します
数値フィルター 比較、平均、上位 10 などの数値に対するさまざまなタイプのフィルター。
テキスト フィルター で始まる、終わる、含むなどのさまざまなフィルター、
ブランクス/ノンブランクス これらのフィルターは、Text Filter Blank を使用して実装できます。
ユーザーは、これらのオプションを使用して、Microsoft Excel でワークシート データを手動でフィルター処理します。

Aspose.Cells のオートフィルター

Aspose.Cells はクラスを提供し、ワークブックこれは Excel ファイルを表します。のワークブッククラスにはワークシート コレクションこれにより、Excel ファイル内の各ワークシートにアクセスできます。

ワークシートは、ワークシートクラス。のワークシートクラスには、ワークシートを管理するためのさまざまなプロパティとメソッドが用意されています。オートフィルターを作成するには、オートフィルターのプロパティワークシートクラス。のオートフィルタープロパティはのオブジェクトですオートフィルターを提供するクラス範囲見出し行を構成するセルの範囲を指定するためのプロパティ。見出し行であるセル範囲にオートフィルターが適用されます。

各ワークシートでは、1 つのフィルター範囲のみを指定できます。これは、Microsoft Excel によって制限されます。カスタム データ フィルタリングの場合は、[AutoFilter.Custom](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#custom(int,%20int,%20java.lang.Object)) 方法。

以下の例では、上記のセクションで Microsoft Excel を使用して作成したのと同じ AutoFilter を Aspose.Cells を使用して作成しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
Workbook workbook = new Workbook("AFData.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
AutoFilter autoFilter = worksheet.getAutoFilter();
autoFilter.setRange("A1:B1");
// Saving the modified Excel file
workbook.save("AFData_out.xls");
// Print message
System.out.println("Process completed successfully");

さまざまな種類のフィルター

Aspose.Cells には、カラー フィルター、日付フィルター、数値フィルター、テキスト フィルター、空白フィルター、なし空白フィルターなど、さまざまな種類のフィルターを適用する複数のオプションが用意されています。

塗りつぶしの色

Aspose.Cells は機能を提供します[addFillColorFilter](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#addFillColorFilter(int,%20int,%20com.aspose.cells.CellsColor,%20com.aspose.cells.CellsColor)セルの塗りつぶしの色のプロパティに基づいてデータをフィルター処理します。以下の例では、シートの最初の列に異なる塗りつぶし色を持つテンプレート ファイルを使用して、カラー フィルタリング機能をテストしています。次のファイルをダウンロードして、機能を確認できます。

  1. ColouredCells.xlsx
  2. FilteredColoredCells.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("ColouredCells.xlsx");
// Instantiating a CellsColor object for foreground color
CellsColor clrForeground = workbook.createCellsColor();
clrForeground.setColor(Color.getRed());
// Instantiating a CellsColor object for background color
CellsColor clrBackground = workbook.createCellsColor();
clrBackground.setColor(Color.getWhite());
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call AddFillColorFilter function to apply the filter
worksheet.getAutoFilter().addFillColorFilter(0, BackgroundType.SOLID, clrForeground, clrBackground);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredColouredCells.xlsx");
// Print message
System.out.println("Process completed successfully");
日にち

2018 年 1 月の日付を持つすべての行をフィルタリングするなど、さまざまなタイプの日付フィルターを実装できます。[addDateFilter](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#addDateFilter(int,%20int,%20int,%20int,%20int,%20int,%20int,%20int)) 関数。この機能のテストには、次のファイルを使用できます。

  1. 日付.xlsx
  2. FilteredDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call AddDateFilter function to apply the filter
worksheet.getAutoFilter().addDateFilter(0, DateTimeGroupingType.MONTH, 2018, 1, 0, 0, 0, 0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredDate.xlsx");
// Print message
System.out.println("Process completed successfully");
動的日付

年に関係なく 1 月の日付を持つすべてのセルのように、日付に基づく動的フィルターが必要になる場合があります。この場合、DynamicFilter 関数は、次のサンプル コードで指定されているように使用されます。次のファイルをテストに使用できます。

  1. 日付.xlsx
  2. FilteredDynamicDate.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call DynamicFilter function to apply the filter
worksheet.getAutoFilter().dynamicFilter(0, DynamicFilterType.JANUARY);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredDynamicDate.xlsx");
番号

カスタム フィルターは、Aspose.Cells を使用して、特定の範囲内の数値を持つセルを選択するように適用できます。次の例は、の使用法を示しています習慣() 数値をフィルタリングする関数。サンプルファイルは以下のリンクからダウンロードできます。

  1. 数値.xlsx
  2. FilteredNumber.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call DynamicFilter function to apply the filter
worksheet.getAutoFilter().dynamicFilter(0, DynamicFilterType.JANUARY);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredDynamicDate.xlsx");
文章

列にテキストが含まれ、特定のテキストを含むセルが選択される場合、フィルター()機能が使えます。次の例では、テンプレート ファイルに国のリストが含まれており、特定の国名を含む行が選択されます。次のコードは、以下のサンプル ファイルを使用してテキストをフィルタリングする方法を示しています。

  1. テキスト.xlsx
  2. FilteredText.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Text.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call Filter function to apply the filter
worksheet.getAutoFilter().filter(0, "Angola");
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredText.xlsx");
ブランクス

列に空白のセルがほとんどないようなテキストが含まれており、空白のセルが存在する行のみを選択するためにフィルタが必要な場合、matchBlanks() 関数は、以下に示すように使用できます。サンプルファイルは以下のリンクからダウンロードできます。

  1. 空白.xlsx
  2. FilteredBlank.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call matchBlanks function to apply the filter
worksheet.getAutoFilter().matchBlanks(0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredBlank.xlsx");
非ブランク

テキストを含むセルをフィルタリングする場合は、MatchNonBlanks 以下に示すようにフィルター関数。サンプルファイルは以下のリンクからダウンロードできます。

  1. 空白.xlsx
  2. FilteredNonBlank.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook("Blank.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Call matchBlanks function to apply the filter
worksheet.getAutoFilter().matchBlanks(0);
// Call refresh function to update the worksheet
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("FilteredBlank.xlsx");
含むカスタム フィルター

Excel には、特定の文字列を含むフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、サンプル ファイル内の名前をフィルタリングすることによって以下に示されています。サンプルファイルは以下のリンクからダウンロードできます。

  1. sourceSampleCountryNames.xlsx
  2. outSourseSampleCountryNames.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.getAutoFilter().custom(0, FilterOperatorType.CONTAINS, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("outSourseSampleCountryNames.xlsx");
NotContains を使用したカスタム フィルター

Excel には、特定の文字列を含まないフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。

  1. sourceSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook("sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.getAutoFilter().custom(0, FilterOperatorType.NOT_CONTAINS, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save("outSourseSampleCountryNames.xlsx");
BeginsWith を使用したカスタム フィルター

Excel には、特定の文字列で始まるフィルター行などのカスタム フィルターが用意されています。この機能は Aspose.Cells で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。

  1. sourceSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// 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 で利用可能で、以下のサンプル ファイルで名前をフィルタリングすることによって以下に示されています。

  1. sourceSampleCountryNames.xlsx.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(srcDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Creating AutoFilter by giving the cells range
worksheet.getAutoFilter().setRange("A1:A18");
// Initialize filter for rows starting with string "Ba"
worksheet.getAutoFilter().custom(0, FilterOperatorType.ENDS_WITH, "ia");
//Refresh the filter to show/hide filtered rows
worksheet.getAutoFilter().refresh();
// Saving the modified Excel file
workbook.save(outDir + "outSourseSampleCountryNames.xlsx");

先行トピック