如何在以编程方式添加到Excel工作表单元格的xlDropDown中获取所选项目的值
本文关键字:xlDropDown 获取 单元格 项目 选项 表单 工作 编程 方式 Excel 添加 | 更新日期: 2023-09-27 18:25:42
以下代码显示了我以编程方式创建下拉列表的方式。它运行良好。但现在我需要得到一个单元格的特定下拉列表的值。
Microsoft.Office.Interop.Excel.DropDowns xlDropDowns;
Microsoft.Office.Interop.Excel.DropDown xlDropDown;
xlDropDowns = ((Microsoft.Office.Interop.Excel.DropDowns)(sheet.DropDowns(Type.Missing)));
xlDropDown = xlDropDowns.Add((double)rag.Left, (double)rag.Top, (double)rag.Width, double)rag.Height, true);
var DropDownList = {"aaaa","bbbb","cccc","dddd"};
int x = 0;
foreach (var item in DropDownList)
{
x++;
xlDropDown.AddItem(item);
}
这就是我尝试获取xlDropDown值的方式。currentCell是我有下拉的单元格
ColumnVal = currentCell.Text; // This didnt give any output
或
var dd = (Microsoft.Office.Interop.Excel.DropDown)currentCell.DropDowns(Type.Missing);
我知道第二个是错误的,因为单元格范围和下拉是两个不同的东西。但我尝试了所有的选择,仍然没有找到任何解决方案。有人请帮我
更清楚地说,我想访问一个特定的单元格(currentCell)及其包含的xldropdown,然后从中获取值
首先,您需要引用刚刚添加的下拉列表:
*假设只有一个下降,下面的
xlDropDown = ((Excel.DropDown)(xlDropDowns.Item(1)));
那么您需要访问CCD_ 2的CCD_。
示例:
if (xlDropDown.Value > 0)
{
sht.get_Range("A1").Value = xlDropDown.get_List(xlDropDown.Value);
}
else
{
throw new Exception("Nothing was selected yet");
}
识别下降:
您可以为xlDropDowns
集合上的每个循环创建一个,并获取每个xlDropDown
的.Name
和.ListIndex
?
foreach (Excel.DropDown xlDD in xlDropDowns)
{
MessageBox.Show(xlDD.Name + ", " + xlDD.ListIndex);
}