C#: Add or Remove AutoFilter in Excel

Excel's AutoFilter is a simple yet effective tool for managing data, especially when working with large datasets. By using AutoFilters, you can quickly narrow down your focus to specific subsets of information, making it easier to identify trends, make decisions, and keep your spreadsheets organized. Upon completion of the analysis, you may need to remove the AutoFilters to restore visibility to the full dataset. In this article, you will learn how to add or remove AutoFilter in Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add AutoFilter to Excel Cells in C#

Spire.XLS for .NET allows you to apply AutoFilter on a specific cell range through the Worksheet.AutoFilters.Range property. The following are the detailed steps:

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Add an AutoFilter to a specified cell range using Worksheet.AutoFilters.Range property.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace AddAutoFilter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Data.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Create an AutoFilter in the sheet and specify the range to be filtered
            sheet.AutoFilters.Range = sheet.Range["A1:C1"];

            //Save the result file
            workbook.SaveToFile("ExcelAutoFilter.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#: Add or Remove AutoFilter in Excel

Apply Date AutoFilter in Excel in C#

If you need to explore information related to specific dates or time, you can apply a date filter to the selected range using the Workbook.AutoFilters.AddDateFilter(IAutoFilter column, DateTimeGroupingType dateTimeGroupingType, int year, int month, int day, int hour, int minute, int second) method. The following are detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Add an AutoFilter to a specified range using Workbook.AutoFilters.Range property.
  • Get the column to be filtered.
  • Call the Workbook.AutoFilters.AddDateFilter() method to add a date filter to the column to filter data related to a specified year/month/date, etc.
  • Apply the filter using Workbook.AutoFilters.Filter() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.Spreadsheet.AutoFilter;

namespace AddAutoFilter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Data.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Create an auto filter in the sheet and specify the range to be filtered
            sheet.AutoFilters.Range = sheet.Range["A1:A12"];

            //Get the column to be filtered
            IAutoFilter filtercolumn = sheet.AutoFilters[0];

            //Add a date filter to filter data related to February 2022
            sheet.AutoFilters.AddDateFilter(filtercolumn, DateTimeGroupingType.Month, 2022, 2, 0, 0, 0, 0);

            //Apply the filter
            sheet.AutoFilters.Filter();

           //Save the result file
            workbook.SaveToFile("DateAutoFilter.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#: Add or Remove AutoFilter in Excel

Apply Custom AutoFilter in Excel in C#

The Workbook.AutoFilters.CustomFilter(FilterColumn column, FilterOperatorType operatorType, Object criteria) method allows you to create custom filters based on certain criteria. For example, you can filter data that contains specific text. The following are detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Add an AutoFilter to a specified range using Workbook.AutoFilters.Range property.
  • Get the column to be filtered.
  • Add a custom filter to the column to filter data containing the specified string using Workbook.AutoFilters.CustomFilter() method.
  • Apply the filter using Workbook.AutoFilters.Filter() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.AutoFilter;

namespace AddAutoFilter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Data.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Create an auto filter in the sheet and specify the range to be filtered
            sheet.AutoFilters.Range = sheet.Range["G1:G12"];

            //Get the column to be filtered
            FilterColumn filtercolumn = (FilterColumn)sheet.AutoFilters[0];

            //Add a custom filter to filter data containing the string "Grocery"
            string strCrt = "Grocery";
            sheet.AutoFilters.CustomFilter(filtercolumn, FilterOperatorType.Equal, strCrt);

            //Apply the filter
            sheet.AutoFilters.Filter();

           //Save the result file
            workbook.SaveToFile("CustomAutoFilter.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#: Add or Remove AutoFilter in Excel

Remove AutoFilter in Excel in C#

In addition to adding AutoFilters in Excel files, Spire.XLS for .NET also support removing or deleting the AutoFilters from Excel through the Worksheet.AutoFilters.Clear() method. The following are detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[sheetIndex] property.
  • Remove AutoFilter from the worksheet using Worksheet.AutoFilters.Clear() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace AddAutoFilter
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile("CustomAutoFilter.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Delete AutoFilter from the sheet
            sheet.AutoFilters.Clear();

           //Save the result file
            workbook.SaveToFile("RemoveAutoFilter.xlsx", ExcelVersion.Version2016);
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.