How to get value from Excel dropdown using C #

I am looking for code to open and read an Excel file, any version of Excel, including 2010. One of my columns has a drop-down menu. I need to get the value of the selected item from the drop down list. Ultimately, I want these values โ€‹โ€‹to be added to the business object.

If anyone has an exchange code, please let me know.

I am using C # and Visual Studio 2010.

Thank.

+3
source share
1 answer

I know VBA both for compiling ActiveX and for the drop-down list of forms, and on the basis of this I can give you very inexperienced notes for C # for the drop-down list of forms, the combo eludes me.

: 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, , :

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

+1

Source: https://habr.com/ru/post/1764761/


All Articles