Using C# and the Excel Autofilter

The following article provides an overview of how to filter ListObjects by using the Excel Auto Filter.  This example and code below uses the following spreadsheet as the basis
 
Excel_Sheet.png
 
The following C# code will filter "Carrots" and "Potato" using an Array
  1. private void ExcelFilterExample()  
  2. {  
  3.     String[] FilterList = {"Carrots","Potato"};  
  4.     Excel.Workbook oWB = Globals.ThisAddIn.Application.Workbooks.Open(@"C:\Users\Documents\NameOfWorkbook.xls");  
  5.     Excel.Worksheet oWS = oWB.Worksheets[1];  
  6.     oWS.ListObjects.AddEx (Excel.XlListObjectSourceType.xlSrcRange, oWS.UsedRange, System.Type.Missing  ,Excel.XlYesNoGuess.xlYes).Name = "VeggieList";  
  7.      oWS.ListObjects["VeggieList"].Range.AutoFilter(2, FilterList, Excel.XlAutoFilterOperator.xlFilterValues   );  
  8. }  
Please note every attempt has been made to get the code 100% accurate.  Please pardon any typos or errors.


Similar Articles