【发布时间】:2016-01-23 19:32:34
【问题描述】:
这是我编写的在 Excel 单元格中制作下拉列表的方法,方法是从同一工作表/工作簿中的某个范围内获取可能的值。
private void MakeDropDownList(string strSrcSheetName, string strDestSheetName, string strSrcRange, string strDestCell)
{
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);
}
}
要使用它,我可以这样调用它:
MakeDropDownList("Units", "ReceiveBonds", "B1:B5", "E9:E18");
但是,最终的结果是这样的:
如您所见,DropDown List 小于E Column Width,更糟糕的是,当我从 DropDown List 中选择一个值后,单元格保持其原始值是Choose Unit。
我是不是做错了什么?
我想要什么:
- 要么使此下拉列表看起来像 Excel 的常规验证下拉列表,要么。
- 能够触发事件将列表中的选定值设置到相应的单元格中。
【问题讨论】:
-
除非您反对实际使用 Excel 的内置验证下拉菜单...this should point you in the write direction.
-
@DanielCook 在 `cell.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, values, missing);'这一行上,我收到 COM Exception 0x800A03EC
标签: c# excel drop-down-menu vsto