如何使用C++筛选空白或非空白
Contents
[
Hide
]
可能的使用场景
在 Excel 中对数据进行筛选是一项有价值的工具,通过使用户能够根据其条件专注于特定数据子集, 提升了数据分析、探索和演示功能, 使整体的数据处理和解释过程更加高效和有效。
如何在 Excel 中筛选空白或非空白
在 Excel 中,您可以轻松地使用筛选选项来筛选空白或非空白。以下是操作步骤:
如何在 Excel 中筛选空白
- 选择范围:点击列标题的字母以选择整个列或选择要筛选空白的范围。
- 打开筛选菜单:转到功能区中的“数据”选项卡。
- 筛选选项:单击“筛选”按钮。这将在所选范围添加筛选箭头。
- 筛选空白:点击要筛选空白的列中的筛选箭头。取消选择除“空白”之外的所有选项,然后单击“确定”。这将仅显示该列中的空白单元格。
- 结果如下:
如何在 Excel 中筛选非空白
- 选择范围:单击列标题的字母以选择整个列,或选择要筛选非空的范围。
- 打开筛选菜单:转到功能区中的“数据”选项卡。
- 筛选选项:单击“筛选”按钮。这将在所选范围添加筛选箭头。
- 筛选非空:单击要筛选非空的列中的筛选箭头。取消选择除“非空”或“自定义”之外的所有选项,并相应设置条件。单击“确定”。这将仅显示该列中非空的单元格。
- 结果如下:
如何使用Aspose.Cells进行筛选空白
如果一列包含文本,且少数单元格为空,需要筛选出仅包含空白单元格的行,可以使用 AutoFilter.MatchBlanks(int fieldIndex) 和 AutoFilter.AddFilter(int fieldIndex, string criteria) 函数,如下所示。
请参阅以下示例代码,该代码加载包含一些虚拟数据的示例Excel文件。示例代码使用三种方法来筛选空白。然后,将工作簿另存为输出Excel文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Open the Excel file
Workbook workbook(u"sample.xlsx");
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Method 1: Call MatchBlanks function to apply the filter
// worksheet.GetAutoFilter().MatchBlanks(1);
// Method 2: Call AddFilter function and set criteria to ""
// worksheet.GetAutoFilter().AddFilter(1, u"");
// Method 3: Call AddFilter function and set criteria to nullptr
worksheet.GetAutoFilter().AddFilter(1, nullptr);
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Saving the modified Excel file
workbook.Save(u"FilteredBlanks.xlsx");
std::cout << "Excel file modified and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
如何使用Aspose.Cells进行非空白过滤
请参考以下示例代码,加载包含一些虚拟数据的示例Excel文件。加载后,调用 AutoFilter.MatchNonBlanks(int fieldIndex) 函数筛选非空数据,并最终将工作簿保存为输出Excel文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Create a workbook by opening an existing Excel file
Workbook workbook(u"sample.xlsx");
// Access the first worksheet in the workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Call MatchNonBlanks function to apply the filter on the second column (index 1)
worksheet.GetAutoFilter().MatchNonBlanks(1);
// Call refresh function to update the worksheet
worksheet.GetAutoFilter().Refresh();
// Save the modified Excel file
workbook.Save(u"FilteredNonBlanks.xlsx");
std::cout << "Filtered non-blanks saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}