如何找到一个小区的命名范围 - VSTO [英] How to find the named range of a cell - VSTO

查看:188
本文介绍了如何找到一个小区的命名范围 - VSTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经产生了一系列使用C#和VSTO行。我已经基本装入数据几行,并给予每个单元根据NamedRange。我的问题是如何将我,知道开始行和结束行索引,遍历每个单元并检索它的NamedRange。我试着Excel.Range范围=(Excel.Range)m_worksheet.Cells [X,Y]它得到了一系列优质,但后来当我做了range.Name.ToString();我得到系统.__ COM ......而不是名称。
谁能帮助?

I have generated a series of rows using C# and VSTO. I have basically loaded a couple of rows with data and have given each cell a NamedRange. My question is how would I, knowing the beginning row and end row index, traverse each cell and retrieve it's NamedRange. I've tried Excel.Range range = (Excel.Range)m_worksheet.Cells[x,y]; which gets the range fine, but then when I do a range.Name.ToString(); I get "System.__COM...." instead of the name. Can anyone assist?

感谢

推荐答案

下面是示例代码(采取从这里) ,如何通过在Excel命名范围迭代

Here is the sample code (take from here) how you can iterate through named range in Excel.

private Excel.Workbook m_workbook;
object missing = Type.Missing;

   public void testNamedRangeFind()
    {
        m_workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
        int i = m_workbook.Names.Count;
        string address = "";
        string sheetName = "";

        if (i != 0)
        {
            foreach (Excel.Name name in m_workbook.Names)
            {
                string value = name.Value;
                //Sheet and Cell e.g. =Sheet1!$A$1 or =#REF!#REF! if refers to nothing
                string linkName = name.Name;
                //gives the name of the link e.g. sales
                if (value != "=#REF!#REF!")
                {
                    address = name.RefersToRange.Cells.get_Address(true, true, Excel.XlReferenceStyle.xlA1, missing, missing);
                    sheetName = name.RefersToRange.Cells.Worksheet.Name;
                }
                Debug.WriteLine("" + value + ", " + linkName + " ," + address + ", " + sheetName);
            }
        }

    }

这篇关于如何找到一个小区的命名范围 - VSTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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