将Excel数据从下拉到C#对象数组 [英] Read Excel data from Drop down into C# object array

查看:137
本文介绍了将Excel数据从下拉到C#对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C#里我想从Excel表读取数据到C#对象。

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

除了一个小细节,Excel数据从下拉列表中,一切都可以正常运行。

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.

我的代码远远低于:

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

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

只要单元格数据不是源自Excel下拉列表, 。但是一旦数据来自一个下拉列表,这个数据条目将为空。

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.

欣赏任何输入,Danne: - )

Appreciate any input, Danne :-)

推荐答案

出现的问题是因为下拉列表的数据实际上并不存储在Range对象本身中。如果你看看你的WorkSheet对象,你会注意到一个DropDowns方法。这将返回一个DropDowns对象,然后您可以调用Item on获取单个DropDown。从那里你可以在DropDown本身的列表中工作,比如:

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

我没有改变要将项目从DropDown中删除(只添加更多与AddItem),然而获取列表和获取选择方法似乎是一个好的开始。

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.

这篇关于将Excel数据从下拉到C#对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆