How to remove autofilter from an Excel worksheet?

30 Oct 20242 minutes to read

You can remove the AutoFilter from a specified worksheet by setting the FilterRange property to null.

The following code example illustrates how to remove the AutoFilter from a worksheet.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet worksheet = workbook.Worksheets[0];

    //Remove autofilter from worksheet
    IAutoFilters filter = worksheet.AutoFilters;
    filter.FilterRange = null;

    //Saving the workbook as stream
    FileStream OutputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
    workbook.SaveAs(OutputStream);
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];

    //Remove autofilter from worksheet
    IAutoFilters filter = worksheet.AutoFilters;
    filter.FilterRange = null;

    //Saving the workbook
    workbook.SaveAs("Output.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    'Remove autofilter from the worksheet
    Dim filter As IAutoFilters = worksheet.AutoFilters
    filter.FilterRange = Nothing

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using