VSTO是否可以访问Excel Filter复选标记的内容?

是否有可能以编程方式检索和设置截图中的复选标记?

在这里输入图像说明

TIA

我在Visual Studio 2010中创build了一个控制台应用程序,并在Microsoft Excel X.0 Object Library中添加了一个COM引用。 X将根据您安装的Excel版本而有所不同。 这里是你应该使用的代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = Microsoft.Office.Interop.Excel; namespace OpenExcel { class Program { static void Main(string[] args) { Excel.Application xlApp = null; try { xlApp = new Excel.Application(); xlApp.Visible = true; Excel.Workbook xlWb = xlApp.Workbooks.Open(@"C:\Users\joe.bob\Desktop\tmp\Book1.xlsx"); //include the path to your real Excel file Excel.Worksheet xlWSht = xlWb.Sheets["Sheet1"]; //include the name of your worksheet where you have the data //here the data is on a Worksheet called Sheet1 int startRow = 9; //this is the row where the data starts string startCol = "A"; //the start column int endRow = 15; string endCol = "F"; int filterColumn = 6; //this is an OFFSET string[] filterList = new string[] { "DTP-3432", "DTP-343243" }; //this is the list of values you want to show Excel.Range myData = xlWSht.get_Range(startCol + startRow, endCol + endRow); myData.AutoFilter(filterColumn, filterList.Length > 0 ? filterList : Type.Missing, Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true); Console.WriteLine("Press a key to quit..."); Console.ReadKey(); } finally { if (xlApp != null) { xlApp.Quit(); } } } //end main } //end program } //end namespace 

欲了解更多信息,请参阅以下链接: http : //msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.range.autofilter%28v=office.15%29.aspx

http://blogs.msdn.com/b/erikaehrli/archive/2005/10/27/excelmanagedautofiltering.aspx

这里是如何拉入VBA中的选中项目。 它将很容易转换为C#/ VSTO:

 Sub GetFilterCriteria() Dim i As Long Dim afCriteria1 As Variant afCriteria1 = ActiveSheet.AutoFilter.Filters(1).Criteria1 For i = LBound(afCriteria1) To UBound(afCriteria1) Debug.Print afCriteria1(i) Next i End Sub