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
The following C# code will filter "Carrots" and "Potato" using an Array
- private void ExcelFilterExample()
- {
- String[] FilterList = {"Carrots","Potato"};
- Excel.Workbook oWB = Globals.ThisAddIn.Application.Workbooks.Open(@"C:\Users\Documents\NameOfWorkbook.xls");
- Excel.Worksheet oWS = oWB.Worksheets[1];
- oWS.ListObjects.AddEx (Excel.XlListObjectSourceType.xlSrcRange, oWS.UsedRange, System.Type.Missing ,Excel.XlYesNoGuess.xlYes).Name = "VeggieList";
- oWS.ListObjects["VeggieList"].Range.AutoFilter(2, FilterList, Excel.XlAutoFilterOperator.xlFilterValues );
- }
Please note every attempt has been made to get the code 100% accurate. Please pardon any typos or errors.