如何找到命名范围的单元格 - VSTO [英] How to find the named range of a cell - VSTO

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

问题描述

我已经使用C#和VSTO生成了一系列行。我已经基本上加载了几行数据,并给每个单元格一个NamedRange。我的问题是,如何知道开始行和结束行索引,遍历每个单元格并检索它的NamedRange。我试过Excel.Range范围=(Excel.Range)m_worksheet.Cells [x,y];这得到范围很好,但是当我做一个range.Name.ToString();我得到System .__ COM ....而不是名字。
可以帮助吗?



谢谢

解决方案

是示例代码(从此处获取)如何在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(m_workbook.Names中的Excel.Name名称)
{
string value = name.Value ;
// Sheet and Cell例如= Sheet1!$ A $ 1或=#REF!#REF!如果没有指定
string linkName = name.Name;
//给出链接的名称,例如销售
if(value!==#REF!#REF!)
{
address = name.RefersToRange.Cells.get_Address(true,true,Excel.XlReferenceStyle.xlA1,missing , 失踪);
sheetName = name.RefersToRange.Cells.Worksheet.Name;
}
Debug.WriteLine(+ + +,+ linkName +,+ address +,+ sheetName);
}
}

}


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?

Thanks

解决方案

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天全站免登陆