且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#获取Excel下拉列表中的值

更新时间:2023-02-11 20:22:19

我知道ActiveX组合和表单下拉列表中的VBA,并且基于此,我可以为表单下拉列表中的c#提供一些非常无效的注释,该组合还包括我。

I know the VBA for both the ActiveX combo and the forms dropdown, and based on that, I can give you some very inexpert notes for c# for the forms dropdown, the combo eludes me as yet.

使用以下注释: http://support.microsoft。 com / kb / 302084

//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Open("C:\\Docs\\Book1.xls"));
//3rd Sheet
oSheet = (Excel._Worksheet)oWB.Sheets.get_Item(3);

//This will return an index number
var i = oSheet.Shapes.Item("Drop Down 1").ControlFormat.Value;
//This will return the fill range
var r = oSheet.Shapes.Item("Drop Down 1").ControlFormat.ListFillRange;
oRng = oSheet.get_Range(r);
//This will return the value of the dropdown, based on the index
//and fillrange
var a =oRng.get_Item(i).Value;

//Just to check
textBox1.Text = a; 

这可能有助于ActiveX组合,但我只有一半可以工作:

This may help with an ActiveX combo, but I have only half got it to work:

using MSForm = Microsoft.Vbe.Interop.Forms;

<...>
Excel.OLEObject cbOLEObj = (Excel.OLEObject)workSheet.OLEObjects("ComboBox1");
MSForm.ComboBox ComboBox1 = (MsForm.ComboBox) cbOLEObj.Object; 
Console.WriteLine(ComboBox1.Text);

来自: http://www.eggheadcafe.com/community/aspnet/66/10117559/excel-get-value-from- a-combobox.aspx