Set Excel AutoFilter in C# and VB.NET
The following example shows how to create AutoFilter on rows in a specific cell range of an Excel worksheet with C# and VB.NET using the GemBox.Spreadsheet library.
using GemBox.Spreadsheet;
// If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var workbook = ExcelFile.Load("SampleData.xlsx");
var worksheet = workbook.Worksheets["Data"];
int rowCount = worksheet.Rows.Count;
// Specify range which will be filtered.
var filterRange = worksheet.Cells.GetSubrangeAbsolute(0, 0, rowCount, 4);
// Show only rows which satisfy following conditions:
// - 'Departments' value is either "Legal" or "Marketing" or "Finance" and
// - 'Names' value contains word "Fred" and
// - 'Salaries' value is in the top 20 percent of all 'Salaries' values.
// Shown rows are then sorted by 'Salaries' values in the descending order.
filterRange.Filter()
.ByValues(0, "Legal", "Marketing", "Finance")
.ByCustom(1, FilterOperator.Equal, "*Fred*")
.ByTop10(3, true, true, 20)
.SortBy(3, true)
.Apply();
workbook.Save("Filtering.xlsx");Imports GemBox.Spreadsheet
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
Dim workbook = ExcelFile.Load("SampleData.xlsx")
Dim worksheet = workbook.Worksheets("Data")
Dim rowCount As Integer = worksheet.Rows.Count
' Specify range which will be filtered.
Dim filterRange = worksheet.Cells.GetSubrangeAbsolute(0, 0, rowCount, 4)
' Show only rows which satisfy following conditions:
' - 'Departments' value is either "Legal" or "Marketing" or "Finance" and
' - 'Names' value contains word "Fred" and
' - 'Salaries' value is in the top 20 percent of all 'Salaries' values.
' Shown rows are then sorted by 'Salaries' values in the descending order.
filterRange.Filter() _
.ByValues(0, "Legal", "Marketing", "Finance") _
.ByCustom(1, FilterOperator.Equal, "*Fred*") _
.ByTop10(3, True, True, 20) _
.SortBy(3, True) _
.Apply()
workbook.Save("Filtering.xlsx")
End Sub
End ModuleOutput

Note that filtered rows are hidden in all output file formats, but the active Filter is written only to the XLSX file format.
