データフィルタリング

データの自動フィルタリング

自動フィルタリングは、リスト内で表示したいアイテムのみを素早く選択できる最も簡単な方法です。自動フィルタ機能により、リスト内のアイテムを特定の基準に従ってフィルタリングできます。テキスト、数値、または日付に基づいてフィルタリングできます。

Microsoft Excelの自動フィルタ

Microsoft Excelで自動フィルタ機能を有効にするには:

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

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

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

自動フィルタのオプションには次のものがあります:

オプション 説明
リスト内のすべてのアイテムを一度に表示します。
contains/not containsなどのカスタマイズされたフィルタ条件を指定します。
塗りつぶしの色に基づいてフィルタリングします。
異なる日付の基準に基づいて行をフィルタリングします。
Number Filters 比較、平均、上位10などの数字に基づく異なるタイプのフィルタを適用します。
begins with、ends with、containsなどの異なるフィルタを指定します。
Text Filter Blankを介してこれらのフィルタを実装できます。
Microsoft Excelのユーザーは、これらのオプションを使用してワークシートデータを手動でフィルタリングします。

Aspose.CellsのAutofilter

Aspose.CellsはExcelファイルを表すクラス、Workbookを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるWorksheetCollectionが含まれています。

ワークシートは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-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関数を提供します。以下の例では、シートの最初の列に異なる塗りつぶし色が含まれるテンプレートファイルを使用して、色のフィルタリング機能をテストしています。機能を確認するために以下のファイルをダウンロードできます。

  1. ColouredCells.xlsx
  2. FilteredColouredCells.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");
日付

1月2018年に日付が含まれるすべての行をフィルタリングするなど、さまざまな種類の日付フィルタが実装されることがあります。以下のサンプルコードでは、addDateFilter関数を使用してこのフィルタをデモンストレーションしています。この機能をテストするために以下のファイルを使用できます。

  1. Date.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. Date.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を使用してカスタムフィルタを適用することができます。指定された範囲内の数値を持つセルを選択するためにcustom()関数の使用例を以下に示します。サンプルファイルを以下のリンクからダウンロードできます。

  1. Number.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");
テキスト

列にテキストが含まれ、特定のテキストを含むセルを選択する必要がある場合、filter()関数を使用できます。以下の例では、テンプレートファイルに国のリストが含まれており、特定の国名を含む行が選択されるようになっています。以下のサンプルファイルを使用して、以下のコードによるテキストのフィルタリングをデモンストレーションしています。

  1. Text.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. Blank.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. Blank.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");
Contains カスタムフィルタ

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

  1. sourseSampleCountryNames.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. sourseSampleCountryNames.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. sourseSampleCountryNames.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. sourseSampleCountryNames.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");

高度なトピック