简体   繁体   English

从下拉列表中读取Excel数据到C#对象数组中

[英]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. 从C#内部,我试图将数据从Excel工作表读取到C#对象中。

Everything works fine except one small detail, Excel data from drop down lists. 除一个小细节(下拉列表中的Excel数据)外,其他所有内容均正常运行。

Somehow the result in the array is null for every corresponding Excel drop down list entry. 对于每个对应的Excel下拉列表条目,数组中的结果以某种方式为null。

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. 只要单元格数据不是来自Excel下拉列表valuearray,就可以获取正确的值。 But as soon as the data comes from a drop down list the valuearray entry is null. 但是,一旦数据来自下拉列表,则valuearray条目为null。

Appreciate any input, Danne :-) 感谢任何输入,Danne :-)

The issue you're seeing occurs because the data for a dropdown isnt actually stored in the Range object itself. 出现此问题是因为下拉列表的数据实际上没有存储在Range对象本身中。 If you take a look at your WorkSheet object you'll notice a DropDowns method. 如果您查看WorkSheet对象,您会注意到DropDowns方法。 This returns a DropDowns object which you can then call Item on to get an individual DropDown. 这将返回一个DropDowns对象,您可以随后调用Item以获得单个DropDown。 From there you can work against the list within the DropDown itself like: 从那里,您可以像在DropDown本身中那样处理列表:

(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. 我没有进行任何改动来深入研究从DropDown中获取项目(仅通过AddItem添加更多内容),但是Get List和Get Selected方法似乎是一个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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