Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 19, 2021 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d4ed7d6727eb31ca859668af927ffa2c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d4ed7d6727eb31ca859668af927ffa2c to your computer and use it in GitHub Desktop.
Add or Remove AutoFilter in Excel File (XLSX/XLS) using C#
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range of the heading row
worksheet.AutoFilter.Range = "A1:B1";
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Instantiating a Workbook object containing sample data
Workbook workbook = new Workbook(dataDir + "sourseSampleCountryNames.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Creating AutoFilter by giving the cells range
worksheet.AutoFilter.Range = "A1:A18";
// Initialize filter for rows containing string "Ba"
worksheet.AutoFilter.Custom(0, FilterOperatorType.Contains, "Ba");
//Refresh the filter to show/hide filtered rows
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(dataDir + "outSourseSampleCountryNames.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(dataDir + "Number.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call Custom function to apply the filter
worksheet.AutoFilter.Custom(0, FilterOperatorType.GreaterOrEqual, 5, true, FilterOperatorType.LessOrEqual, 10);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(dataDir + "FilteredNumber.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(dataDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call AddDateFilter function to apply the filter
worksheet.AutoFilter.AddDateFilter(0, DateTimeGroupingType.Month, 2018, 1, 0, 0, 0, 0);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(dataDir + "FilteredDate.xlsx");
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(dataDir + "Date.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Call DynamicFilter function to apply the filter
worksheet.AutoFilter.DynamicFilter(0, DynamicFilterType.January);
// Call refresh function to update the worksheet
worksheet.AutoFilter.Refresh();
// Saving the modified Excel file
workbook.Save(dataDir + "FilteredDynamicDate.xlsx");
// Instantiating a Workbook object
// Opening the Excel file
Workbook workbook = new Workbook(dataDir + "output.xlsx");
// Iterate through each worksheet
foreach (var sheet in workbook.Worksheets)
{
// Remove AutoFilters
sheet.RemoveAutoFilter();
}
// Save the workbook
workbook.Save(dataDir + "AutoFilter_Removed.xlsx", SaveFormat.Xlsx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment