数据筛选与C++
自动筛选数据
自动筛选是选择仅显示列表中您想要显示的项的最快方法。自动筛选功能允许用户根据一组标准过滤列表中的项目。根据文本、数字或日期进行筛选。
Microsoft Excel 中的自动筛选
要在 Microsoft Excel 中启用自动筛选功能:
- 单击工作表中的标题行。
- 从数据菜单中选择筛选,然后选择自动筛选。
在工作表上应用自动筛选时,过滤开关 (黑色箭头) 会出现在列标题右侧。
- 单击筛选箭头,以查看筛选选项列表。
一些自动筛选选项包括:
选项 | 描述 |
---|---|
All | 在列表中显示所有项目一次。 |
Custom | 自定义包含/不包含等筛选条件 |
Filter by Color | 基于填充颜色的筛选 |
Date Filters | 基于不同日期标准的行筛选 |
Number Filters | 在数字上应用不同类型的筛选,例如比较,平均值和前10名等。 |
Text Filters | 不同的筛选,如以…开始、以…结束、包含等。 |
Blanks/Non Blanks | 这些筛选可以通过文本筛选空白值实现。 |
用户可以使用这些选项手动筛选其 Microsoft Excel 工作表中的数据。
Aspose.Cells 自动筛选
Aspose.Cells提供一个Workbook
类,代表一个Excel文件。Workbook
类包含一个Worksheets
集合,可访问Excel文件中的每个工作表。
工作表由Worksheet
类表示。Worksheet
类提供丰富的属性和方法来管理工作表。要创建自动筛选,使用Worksheet
类的AutoFilter
属性。AutoFilter
属性是AutoFilter
类的对象,提供用于指定标题行范围的Range
属性。自动筛选应用于标题行所在的单元格范围。
在每个工作表中,您只能指定一个筛选范围。这是Microsoft Excel的限制。如需自定义数据筛选,请使用AutoFilter.Custom
方法。
在下面的示例中,我们使用 Aspose.Cells 创建了与上一节中使用 Microsoft Excel 创建的相同的自动筛选。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Creating AutoFilter by giving the cells range of the heading row
worksheet.GetAutoFilter().SetRange(u"A1:B1");
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "AutoFilter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
不同类型的筛选
Aspose.Cells 提供了多种选项,应用不同类型的筛选,如颜色筛选、日期筛选、数字筛选、文本筛选、空白筛选和非空白筛选。
填充色
Aspose.Cells提供AddFillColorFilter
函数,用于根据单元格的填充颜色进行筛选。在以下示例中,使用一个模板文件测试颜色筛选功能,该文件在第一列包含不同的填充颜色。可以从以下链接下载示例文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Instantiating a Workbook object
Workbook workbook(srcDir + u"ColouredCells.xlsx");
// Instantiating a CellsColor object for foreground color
CellsColor clrForeground = workbook.CreateCellsColor();
clrForeground.SetColor(Color::Red());
// Instantiating a CellsColor object for background color
CellsColor clrBackground = workbook.CreateCellsColor();
clrBackground.SetColor(Color::White());
// 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(outDir + u"FilteredColouredCells.xlsx");
std::cout << "Filter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
日期
可以实现不同类型的日期筛选,例如筛选所有2018年1月的日期行。以下示例代码演示如何使用AddDateFilter
函数实现此筛选。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Date.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"FilteredDate.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Access 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();
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Date filter applied and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
动态日期
有时需要根据日期实现动态筛选,例如筛选所有在一月的日期,无论年份。在此情况下,使用DynamicFilter
函数,参考以下示例代码。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Date.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"FilteredDynamicDate.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Call DynamicFilter function to apply the filter
worksheet.GetAutoFilter().Dynamic_Filter(0, DynamicFilterType::January);
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Dynamic filter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
数字
可以使用Aspose.Cells应用自定义筛选,例如筛选数字范围内的单元格。以下示例演示如何使用Custom()
函数实现数字筛选。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"Number.xlsx";
// Path of output excel file
U16String outputFilePath = outDir + u"FilteredNumber.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Call Custom function to apply the filter
worksheet.GetAutoFilter().Custom(0, FilterOperatorType::GreaterOrEqual, 5, true, FilterOperatorType::LessOrEqual, 10);
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Filter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
文本
如果列中包含文本,并且要筛选包含特定文本的单元格,可以使用Filter()
函数。在以下示例中,模板文件包含国家列表,筛选包含特定国家名称的行。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Text.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"FilteredText.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Call Filter function to apply the filter
worksheet.GetAutoFilter().Filter(0, u"Angola");
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Filter applied and file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
空白
如果列中包含文本且部分单元格为空,且需要筛选出那些空单元格所在行,可以使用MatchBlanks()
函数,示范如下。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Instantiating a Workbook object
Workbook workbook(srcDir + u"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(outDir + u"FilteredBlank.xlsx");
std::cout << "Filter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
非空白
当需要筛选包含任何文本的单元格时,使用MatchNonBlanks
筛选函数,示范如下。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook object and open the Excel file
Workbook workbook(srcDir + u"Blank.xlsx");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Call MatchNonBlanks function to apply the filter
worksheet.GetAutoFilter().MatchNonBlanks(0);
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Save the modified Excel file
workbook.Save(outDir + u"FilteredNonBlank.xlsx");
std::cout << "Non-blank filter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
包含自定义筛选
Excel 提供了自定义筛选功能,例如筛选包含特定字符串的行。Aspose.Cells 中也提供了此功能,并且通过下面的示例演示了对样本文件中的名称进行筛选。示例文件如下。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"sourseSampleCountryNames.xlsx";
// Path of output excel file
U16String outputFilePath = outDir + u"outSourseSampleCountryNames.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Creating AutoFilter by giving the cells range
worksheet.GetAutoFilter().SetRange(u"A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.GetAutoFilter().Custom(0, FilterOperatorType::Contains, u"Ba");
// Refresh the filter to show/hide filtered rows
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "AutoFilter applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
不包含特定字符串的自定义筛选
Excel提供了自定义筛选功能,例如筛选不包含特定字符串的行。这一功能在Aspose.Cells中可用,并通过以下演示在示例文件中筛选名称。示例文件可从以下链接下载。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"sourseSampleCountryNames.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"outSourseSampleCountryNames.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Creating AutoFilter by giving the cells range
worksheet.GetAutoFilter().SetRange(u"A1:A18");
// Initialize filter for rows containing string "Ba"
worksheet.GetAutoFilter().Custom(0, FilterOperatorType::NotContains, u"Be");
// Refresh the filter to show/hide filtered rows
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "File filtered and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
以指定字符串开头的自定义筛选
Excel提供了自定义筛选功能,例如筛选以特定字符串开头的行。这一功能在Aspose.Cells中可用,并通过以下演示在示例文件中筛选名称。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"sourseSampleCountryNames.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"outSourseSampleCountryNames.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Creating AutoFilter by giving the cells range
worksheet.GetAutoFilter().SetRange(u"A1:A18");
// Initialize filter for rows starting with string "Ba"
worksheet.GetAutoFilter().Custom(0, FilterOperatorType::BeginsWith, u"Ba");
// Refresh the filter to show/hide filtered rows
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
以指定字符串结尾的自定义筛选
Excel 提供了自定义筛选功能,例如筛选以特定字符串结尾的行。Aspose.Cells 中也提供了此功能,并且通过下面的示例演示了对下面给出的样本文件中的名称进行筛选。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"sourseSampleCountryNames.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"outSourseSampleCountryNames.xlsx";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Creating AutoFilter by giving the cells range
worksheet.GetAutoFilter().SetRange(u"A1:A18");
// Initialize filter for rows end with string "ia"
worksheet.GetAutoFilter().Custom(0, FilterOperatorType::BeginsWith, u"ia");
// Refresh the filter to show/hide filtered rows
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(outputFilePath);
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}