如何从C#或vb.net读取Excel的下拉列表或复选框的值? [英] How do I read the values of Excel dropdowns or checkboxes from c# or vb.net?

查看:895
本文介绍了如何从C#或vb.net读取Excel的下拉列表或复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Microsoft.Office.Interop.Excel读取工作表的单元格的值,但我无法找到显示了如何读取下拉菜单,复选框和选项按钮的信息。

I'm using Microsoft.Office.Interop.Excel to read the values of cells of a worksheet, but I'm unable to find information that shows how to read dropdowns, checkboxes and option buttons.

谢谢!

推荐答案

显然直接访问的下拉列表中收集<一href="http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.dropdowns%28VS.80%29.aspx"相对=nofollow>禁止的。一种解决方法是访问包含下拉单元格的验证性,得到它的配方,然后解析出列表的位置。

Apparently accessing the DropDowns collection directly is verboten. A workaround is to access the Validation property of the cell containing the dropdown, get it's formula and then parse out the location of the list.

Excel.Range dropDownCell = (Excel.Range)ws.get_Range("A1", "A1"); //cell containing dropdown
string formulaRange = dropDownCell.Validation.Formula1;
string[] splitFormulaRange = formulaRange.Substring(1,formulaRange.Length-1).Split(':');

Excel.Range valRange = (Excel.Range)ws.get_Range(splitFormulaRange[0], splitFormulaRange[1]);
for (int nRows = 1; nRows <= valRange.Rows.Count; nRows++) {
    for (int nCols = 1; nCols <= valRange.Columns.Count; nCols++) {
         Excel.Range aCell = (Excel.Range)valRange.Cells[nRows, nCols];
     System.Console.WriteLine(aCell.Value2);
    }
}

这篇关于如何从C#或vb.net读取Excel的下拉列表或复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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