VSTO Excel.DropDown事件

我使用C#VSTOdynamic添加ActiveSheet单元格中的Excel.DropDown。 有没有一种方法来处理更改select事件,而无需将macrosembedded工作簿? 或者我会有兴趣使用Excel的数据validation技术(cell.Validation),大概我需要使用SheetChange事件。 不确定哪一个更有效率? 我正在使用的代码是波纹pipe

var currentSheet = Application.Sheets[strDestSheetName]; var inv = Application.Sheets[strSrcSheetName]; var items = inv.Range[strSrcRange]; var list_items = new List<string>(); foreach (Excel.Range cell in items) { list_items.Add(cell.Value2.ToString()); } Range xlsRange; xlsRange = currentSheet.Range[strDestCell]; Excel.DropDowns xlDropDowns; Excel.DropDown xlDropDown; xlDropDowns = ((Excel.DropDowns)(currentSheet.DropDowns(Missing.Value))); xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true); //Add item into drop down list for (int i = 0; i < list_items.Count; i++) { xlDropDown.AddItem(list_items[i], i + 1); } xlDropDown.OnAction = "SomeMacroCode"; 

你可以这样使用Excel的数据validation技术:

 var activeSheet = (Worksheet) Globals.ThisAddIn.Application.ActiveSheet; int lastUsedCell = activeSheet.UsedRange.Rows.Count; //in this example we dynamicly add drop down list to second colomn string columnName = "B" + lastUsedCell; //the range is from second colomn of first row to last row Range range = activeSheet.Range["B1", columnName]; var list=new List<string>(); list.Add("a"); list.Add("b"); string items= string.Join(",", list); range.Validation.Add(XlDVType.xlValidateList, Type.Missing, XlFormatConditionOperator.xlBetween, items); InsertingTypeNotificationLable.Visible = true; SendButton.Enabled = true; 

也可以在运行时dynamic填充下拉列表,例如,每当用户单击任务窗格中的button时