数据过滤

自动过滤数据

自动筛选是从工作表中仅选择要在列表中显示的项目的最快方法。自动筛选功能允许用户根据设定的标准筛选列表中的项目。根据文本、数字或日期进行过滤。

Microsoft Excel 中的自动筛选器

要激活 Microsoft Excel 中的自动筛选功能:

  1. 单击工作表中的标题行。
  2. 来自数据菜单,选择筛选接着自动过滤.

当您将自动过滤器应用于工作表时,过滤器开关(黑色箭头)出现在列标题的右侧。

  1. 单击过滤器箭头可查看过滤器选项列表。

一些自动过滤器选项是:

选项 描述
全部 一次显示列表中的所有项目。
风俗 自定义过滤条件,如包含/不包含
按颜色筛选 基于填充颜色的过滤器
日期过滤器 根据日期的不同标准过滤行
数字过滤器 不同类型的数字过滤器,如比较、平均值和前 10 名等。
文本过滤器 不同的过滤器,如开头、结尾、包含等,
空白/非空白 这些过滤器可以通过 Text Filter Blank 来实现
用户使用这些选项手动筛选 Microsoft Excel 中的工作表数据。

带 Aspose.Cells 的自动过滤器

Aspose.Cells提供了一个类,工作簿表示一个 Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。

工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。要创建自动过滤器,请使用自动过滤的财产工作表班级。这自动过滤属性是自动过滤类,它提供了范围用于指定构成标题行的单元格范围的属性。自动过滤器应用于作为标题行的单元格范围。

在每个工作表中,您只能指定一个过滤范围。这是受 Microsoft Excel 的限制。对于自定义数据过滤,使用[自动过滤器.自定义](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#custom(int,%20int,%20java.lang.Object)) 方法。

在下面给出的示例中,我们使用 Aspose.Cells 创建了与上一节中使用 Microsoft Excel 创建的相同的自动筛选器。

// 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提供了一个功能[添加填充颜色过滤器](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#addFillColorFilter(int,%20int,%20com.aspose.cells.CellsColor,%20com.aspose.cells.CellsColor)根据单元格的填充颜色属性过滤数据。在下面给出的示例中,使用工作表第一列具有不同填充颜色的模板文件来测试颜色过滤功能。可以下载以下文件来检查功能。

  1. 彩色细胞.xlsx
  2. 过滤彩色单元格.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 月日期的行。以下示例代码演示了此过滤器使用[添加日期过滤器](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#addDateFilter(int,%20int,%20int,%20int,%20int,%20int,%20int,%20int)) 功能。以下文件可用于测试此功能。

  1. 日期.xlsx
  2. 过滤日期.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");
动态日期

有时需要基于日期的动态过滤器,例如所有单元格的日期都在一月份,而不管年份如何。在这种情况下,[动态过滤器](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#dynamicFilter(int,%20int)函数按以下示例代码中给出的方式使用。以下文件可用于测试。

  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. 筛选编号.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. 过滤文本.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");
毛坯

如果一列包含的文本很少有单元格是空白的,并且需要过滤器以仅选择那些存在空白单元格的行,匹配空白() 函数可以如下所示使用。示例文件可以从以下链接下载。

  1. 空白.xlsx
  2. 筛选空白.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");
非空白

当要过滤包含任何文本的单元格时,使用[匹配非空白](https://reference.aspose.com/cells/java/com.aspose.cells/autofilter#matchNonBlanks(int)过滤功能如下所示。示例文件可以从以下链接下载。

  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");

推进主题