无需MS Office创建Excel!使用C ++以编程方式在Excel文件中添加或删除自动筛选

  • 使用C ++在Excel文件中应用自动筛选
  • 使用C ++在Excel文件中添加日期自动筛选
  • 使用C ++在Excel文件中添加动态日期自动筛选
  • 使用C ++在Excel文件中应用自定义自动筛选
  • 使用C ++从Excel文件中删除自动筛选


使用C ++在Excel文件中应用自动筛选

可以在一系列单元格上应用自动筛选。自动过滤器使您可以排序和过滤给定单元格范围内的值。以下是在Excel文件中添加自动筛选的步骤。

  • 首先,使用IWorkbook 类加载Excel文件 。
  • 使用IWorkbook-> GetIWorksheets()-> GetObjectByIndex(Aspose :: Cells :: Systems :: Int32索引) 方法检索要在其中应用自动筛选的工作表。
  • 使用IWorksheet-> GetIAutoFilter()-> SetRange(intrusive_ptrvalue)方法应用自动筛选器。
  • 最后,使用IWorkbook-> Save(intrusive_ptrfileName) 方法保存Excel文件 。

下面的示例代码显示了如何使用C ++将自动筛选应用于Excel文件中的一系列单元格。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the source Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book3.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Creating AutoFilter by giving the cell rangeworksheet->GetIAutoFilter()->SetRange(new String("A1:B1"));// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("AutoFilter_out.xlsx")));

无需MS Office创建Excel!使用C ++以编程方式在Excel文件中添加或删除自动筛选

使用C ++在Excel文件中添加日期自动筛选

Excel文件可能包含基于日期的数据。可能会遇到需要根据不同日期过滤和分析数据的情况。因此,在这种情况下,日期过滤器将被证明是有用的。以下是在Excel文件中添加日期自动筛选的步骤。

  • 首先,使用IWorkbook 类加载Excel文件 。
  • 使用IWorkbook-> GetIWorksheets()-> GetObjectByIndex(Aspose :: Cells :: Systems :: Int32索引) 方法检索要在其中应用自动筛选的工作表。
  • 使用IWorksheet->GetIAutoFilter()->AddDateFilter(Aspose::Cells::Systems::Int32 fieldIndex, Aspose::Cells::DateTimeGroupingType dateTimeGroupingType, Aspose::Systems:: Int32 year, Aspose::Cells::Systems::Int32 month, Aspose::Cells::Systems::Int32 day, Aspose::Cells::Systems::Int32 hour, Aspose::Cells::Systems::Int32 minute, Aspose::Cells::Systems::Int32 second) 方法。
  • 要更新工作表,使用IWorksheet->GetIAutoFilter()->Refresh()方法。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码演示了如何使用C ++在Excel文件中添加日期自动筛选。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the source Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book4.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Call AddDateFilter function to apply the filterworksheet->GetIAutoFilter()->AddDateFilter(0, DateTimeGroupingType_Month, 2018, 1, 1, 0, 0, 0);// Call the Refresh function to update the worksheetworksheet->GetIAutoFilter()->Refresh();// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("DateFilter_out.xlsx")));

使用C ++在Excel文件中添加动态日期自动筛选

在某些情况下,可能需要更通用的日期过滤器,例如月份,而不管年份或当前月份之前的月份。对于这种情况,可以使用动态自动筛选来筛选数据。以下是使用动态日期自动筛选器筛选数据的步骤。

  • 首先,使用IWorkbook 类加载Excel文件 。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法检索你想应用自动过滤器的工作表。
  • 使用IWorksheet->GetIAutoFilter()->DynamicFilter (Aspose::Cells::Systems:Int32 fieldIndex, Aspose::Cells::DynamicFilterType dynamicFilterType)方法应用动态过滤器。
  • 要更新工作表,使用IWorksheet->GetIAutoFilter()->Refresh()方法。最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码演示了如何使用C++在Excel文件中添加一个动态日期自动过滤器。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the source Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Book4.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Call DynamicFilter function to apply the filterworksheet->GetIAutoFilter()->DynamicFilter(0, DynamicFilterType_Februray);// Call the Refresh function to update the worksheetworksheet->GetIAutoFilter()->Refresh();// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("DynamicDateAutoFilter_out.xlsx")));

使用C++在Excel文件中应用自定义自动过滤器

如果您想应用一个自定义的自动过滤器,Aspose.Cells for C++ API将为您提供帮助。使用该API,您可以根据您的具体要求在Excel文件中应用一个自定义的自动过滤器。以下是在Excel文件中添加自定义自动过滤器的步骤。

  • 首先,使用ICworkbook类加载Excel文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index)方法检索你想应用自动过滤器的工作表。
  • 使用IWorksheet->GetIAutoFilter()->Custom(Aspose::Cells::Systems::Int32 fieldIndex, Aspose::FilterOperatorType operatorType1, intrusive_ptrcriteria1) 方法应用自定义自动过滤器。
  • 调用IWorksheet->GetIAutoFilter()->Refresh()方法来更新工作表。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存 Excel 文件。

下面的示例代码显示了如何使用C++在Excel文件中添加自定义自动过滤器。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the source Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("sampleCountryNames.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Call Custom function to apply the filterworksheet->GetIAutoFilter()->Custom(0, FilterOperatorType_Equal, new String("Brazil"));// Call the Refresh function to update the worksheetworksheet->GetIAutoFilter()->Refresh();// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("CustomFilter_out.xlsx")));

从Excel文件中删除自动过滤器

除了添加自动过滤器之外,还可以使用Aspose.Cells for C++ API来删除它们。下面是删除Excel文件中自动过滤器的步骤。

  • 首先,使用ICworkbook类加载Excel文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index)方法检索你想删除自动过滤器的工作表。
  • 使用IWorksheet->RemoveAutoFilter()方法删除自动过滤器。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存Excel文件。

下面的示例代码显示了如何使用C++从Excel文件中移除AutoFilter。

// Source directory path.StringPtr srcDir = new String("SourceDirectory\");// Output directory path.StringPtr outDir = new String("OutputDirectory\");// Load the source Excel fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleAutoFilter.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Remove AutoFiltersworksheet->RemoveAutoFilter();// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("RemoveAutoFilter_out.xlsx")));

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月17日
下一篇 2021年4月17日

相关推荐

发表回复

登录后才能评论