如何使用C#获取Excel下拉列表中的值 [英] How to get the value in an Excel dropdown using C#
问题描述
我正在寻找打开和读取Excel文件,任何版本的Excel(包括2010)的代码。我的一个列中有一个下拉列表。我需要在下拉列表中获取所选项目的值。我最终会将这些值填充到一个业务对象中。
I am looking for code to open and read an Excel file, any version of Excel, including 2010. One of my columns has a dropdown in it. I need to get the value of the selected item in the dropdown. I would eventually want to populate these values into a business object.
如果有人有一些代码可以分享,请让我知道。
If anyone has some code to share please let me know.
我正在使用C#和Visual Studio 2010。
I am using C# and Visual Studio 2010.
谢谢。
推荐答案
我知道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
这篇关于如何使用C#获取Excel下拉列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!