如何找到命名范围的单元格 - VSTO [英] How to find the named range of a cell - VSTO
本文介绍了如何找到命名范围的单元格 - VSTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以帮助吗?
谢谢
解决方案
是示例代码(从此处获取)如何在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屋!
查看全文