Filtering information in Excel spreadsheets is an important feature. It enables you to hide irrelevant data and show only the data meeting specific criteria. There might be scenarios where filtering data can prove to be helpful. For example, an organization may want to filter out low-performing products from the sales report to analyze and improve their sales strategies. In this article, you will learn how to add or remove AutoFilter in Excel files using C++.
- C++ API for Adding and Removing AutoFilter in Excel Files
- Apply AutoFilter in Excel Files using C++
- Add Date AutoFilter in Excel Files using C++
- Add Dynamic Date AutoFilter in an Excel File using C++
- Apply Custom AutoFilter in Excel Files using C++
- Remove AutoFilter from Excel Files
C++ API to Adding and Removing AutoFilter in Excel Files
We will use the Aspose.Cells for C++ API for adding and removing AutoFilter in Excel files. It is a native C++ library that allows you to create, read and modify Excel files without requiring Microsoft Excel to be installed. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Apply AutoFilter in Excel Files using C++
You can apply AutoFilter on a range of cells. AutoFilters allow you to sort and filter values in the given range of cells. The following are the steps to add AutoFilter in Excel files.
- Firstly, load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to apply the AutoFilter using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Apply the AutoFilter using the IWorksheet->GetIAutoFilter()->SetRange(intrusive_ptr<Aspose::Cells::Systems::String> value) method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to apply AutoFilter to a range of cells in an Excel file using C++.
Add Date AutoFilter in Excel Files using C++
Excel files may contain data based on dates. You might find yourself in scenarios where you need to filter and analyze data based on different dates. Therefore, a date filter will prove to be helpful in such scenarios. The following are the steps to add date AutoFilter in Excel files.
- Firstly, load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to apply the AutoFilter using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Apply Date AutoFilter using the IWorksheet->GetIAutoFilter()->AddDateFilter(Aspose::Cells::Systems::Int32 fieldIndex, Aspose::Cells::DateTimeGroupingType dateTimeGroupingType, Aspose::Cells::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) method.
- To update the worksheet, use the IWorksheet->GetIAutoFilter()->Refresh() method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code demonstrates how to add date AutoFilter in Excel files using C++.
Add Dynamic Date AutoFilter in an Excel File using C++
There might be cases where you need a more generic date filter, such as the month irrespective of the year or the month before the current month. For scenarios like this, you can use the dynamic AutoFilter to filter the data. The following are the steps to filter data using a dynamic date AutoFilter.
- Firstly, load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to apply the AutoFilter using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Apply DynamicFilter using the IWorksheet->GetIAutoFilter()->DynamicFilter (Aspose::Cells::Systems::Int32 fieldIndex, Aspose::Cells::DynamicFilterType dynamicFilterType) method.
- To update the worksheet, use the IWorksheet->GetIAutoFilter()->Refresh() method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code demonstrates how to add a dynamic date AutoFilter in an Excel file using C++.
Apply Custom AutoFilter in Excel Files using C++
In case you want to apply a custom AutoFilter, Aspose.Cells for C++ API has you covered. Using the API, you can apply a custom AutoFilter in Excel files based on your specific requirements. The following are the steps to add custom AutoFilter in Excel files.
- Firstly, load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to apply the AutoFilter using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Apply the Custom AutoFilter using the IWorksheet->GetIAutoFilter()->Custom(Aspose::Cells::Systems::Int32 fieldIndex, Aspose::Cells::FilterOperatorType operatorType1, intrusive_ptr<Aspose::Cells::Systems::Object> criteria1) method.
- Call the IWorksheet->GetIAutoFilter()->Refresh() method to update the worksheet.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to add custom AutoFilter in Excel files using C++.
Remove AutoFilter from Excel Files
In the previous sections, you learned how to add different AutoFilters in Excel files. In addition to adding AutoFilters, you can also remove them using the Aspose.Cells for C++ API. The following are the steps to remove AutoFilters from Excel files.
- Firstly, load the Excel file using the IWorkbook class.
- Retrieve the worksheet from where you want to remove the AutoFilter using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Remove AutoFilter using the IWorksheet->RemoveAutoFilter() method.
- Finally, save the Excel file using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to remove AutoFilter from an Excel file using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to add and remove AutoFilters from Excel files using C++. Specifically, you have learned how to add Default, Date, Dynamic Date and Custom AutoFilters. You have also seen how to remove AutoFilter using Aspose.Cells for C++ API. The API provides many additional features for working with Excel files. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.