简体   繁体   中英

Read Excel data from Drop down into C# object array

From within C# I'm trying to read data from an Excel sheet into an C# object.

Everything works fine except one small detail, Excel data from drop down lists.

Somehow the result in the array is null for every corresponding Excel drop down list entry.

My code so far below:

 var range = sheet.get_Range("A1", "D3");

 var valuearray = (Object[,])range.get_Value(XlRangeValueDataType.xlRangeValueDefault);

As long as the cell data isn't originating from an Excel drop down list valuearray get the correct values. But as soon as the data comes from a drop down list the valuearray entry is null.

Appreciate any input, Danne :-)

The issue you're seeing occurs because the data for a dropdown isnt actually stored in the Range object itself. If you take a look at your WorkSheet object you'll notice a DropDowns method. This returns a DropDowns object which you can then call Item on to get an individual DropDown. From there you can work against the list within the DropDown itself like:

(assuming: using Excel = Microsoft.Office.Interop.Excel)
Excel.DropDowns allDropDowns = YourWorkSheet.DropDowns(Type.Missing);
Excel.DropDown oneDropdown = allDropDowns.Item(YourIndex);

I've not had a change to dig into getting the items out of the DropDown (only adding more with AddItem) however the Get List and Get Selected methods seem a good place to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM