C# 在Excel中以编程方式制作下拉列表(VSTO)的我的(好的?)方法
下面是我编写的在Excel单元格中创建下拉列表的方法,方法是从同一工作表/工作簿中的某个范围中获取可能的值C# 在Excel中以编程方式制作下拉列表(VSTO)的我的(好的?)方法,c#,excel,drop-down-menu,vsto,C#,Excel,Drop Down Menu,Vsto,下面是我编写的在Excel单元格中创建下拉列表的方法,方法是从同一工作表/工作簿中的某个范围中获取可能的值 private void MakeDropDownList(string strSrcSheetName, string strDestSheetName, string strSrcRange, string strDestCell) { var currentSheet = Application.Sheets[strDestSheetName];
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");
如您所见,下拉列表小于E
列宽,更糟糕的是,在我从下拉列表中选择一个值后,单元格保留其原始值,即选择单位
我做错什么了吗
我想要的是:
- 使此下拉列表看起来像Excel的常规验证下拉列表,或者
- 能够触发事件,将列表中的选定值设置为相应的单元格
列宽即可
if (checkModifiers() && (checkKey(Keys.F3)))
{
Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
Microsoft.Office.Interop.Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)Globals.ThisAddIn.Application.ActiveCell;
int row = rng.Row;
int column = rng.Column;
Range cell = ws.Cells[row, column];
cell.ColumnWidth = 50;
try
{
drd.Add("One");
drd.Add("Two");
drd.Add("Three");
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(ws.DropDowns(Missing.Value)));
xlDropDown = xlDropDowns.Add((double)cell.Left, (double)cell.Top, (double)cell.Width, (double)cell.Height, true);
for (int i = 0; i < drd.Count; i++)
{
xlDropDown.AddItem(drd[i], i+1);
}
}catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
if(checkModifiers()&&(checkKey(key.F3)))
{
工作簿wb=Globals.ThisAddIn.Application.active工作簿;
工作表ws=Globals.ThisAddIn.Application.ActiveSheet;
Microsoft.Office.Interop.Excel.Range rng=(Microsoft.Office.Interop.Excel.Range)Globals.ThisAddIn.Application.ActiveCell;
int row=rng.row;
int column=rng.column;
范围单元格=ws.Cells[行,列];
cell.ColumnWidth=50;
尝试
{
drd.添加(“一”);
drd.添加(“两”);
drd.添加(“三”);
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
xlDropDowns=((Microsoft.Office.Interop.Excel.DropDowns)(ws.DropDowns(Missing.Value));
xlDropDown=xlDropDowns.Add((双)cell.Left,(双)cell.Top,(双)cell.Width,(双)cell.Height,true);
对于(int i=0;i
除非您反对实际使用Excel的内置验证下拉列表@DanielCook在“cell.Validation.Add(XlDVType.xlValidateList,XlDVAlertStyle.xlValidAlertStop,XlFormatConditionOperator.xlBetween,values,missing)”行上,我收到一个COM异常0x800A03EC