Read Excel data from Drop down into C# object arra

2019-08-15 17:53发布

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

1条回答
我只想做你的唯一
2楼-- · 2019-08-15 18:31

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.

查看更多
登录 后发表回答