【问题标题】:My (Good?) Method for making DropDown List in Excel programatically (VSTO)我的(好?)以编程方式在 Excel 中制作下拉列表的方法(VSTO)
【发布时间】: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


【解决方案1】:

只需在前面添加ColumnWidth

        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());
            }
        }

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多