C#编程方式创建和QUOT;命名范围"在工作​​表范围 [英] c#: programmatically create a "named range" in worksheet scope

查看:157
本文介绍了C#编程方式创建和QUOT;命名范围"在工作​​表范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何创建一个命名的范围已经其范围设置为工作的? (我们可以从Excel手工做到这一点,我想有一个去这在代码中)

How can we create a 'named range' that has its scope set to a worksheet? (as we can do this manually from Excel, I guess there is a way to this in code)

设置使用范围的名称。名称属性创建一个工作簿范围的命名范围。我试着用前缀区域名称'<表名称>!'的建议这里,但它不工作。该名称不被设置在所有的方式。

Setting a name using the 'Range.Name' property creates a workbook-scoped named range. I tried prefixing the range name with '<Sheet Name>!' as suggested here, but it doesn't work. The name doesn't get set at all that way.

任何想法,如何能在C#做什么?

Any idea as to how this can be done in C#?

更新(2013年5月16日):
由shahkalpesh答案工作。原始代码(在VS 2010中)我用的是:

Update (2013/05/16): Answer by shahkalpesh worked. The original code (in VS 2010) I used is:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range range = ws.Range[ws.Cells[x,y], ws.Cells[(x + height), (y + width)]];
range.Name = "MyName"; // MyName in workbook scope

这作品修改后的代码是:

The modified code that works is:

ws.Names.Add("MyName", range); // MyName in worksheet scope



谢谢,
idssl。

Thanks, idssl.

推荐答案

假设:结果
Sheet1中有单元格A1:A4,您要创建一个名为范围

Assumptions:
Sheet1 has cells A1:A4, for which you want to create a named range

VBA:

Sheet1.Names.Add("tada", Sheet1.Range("A1:A4")

我想这将是在C#相同(除 ; 末),并通过可选参数空参数

I suppose it will be identical in c# (except the ; at the end) and passing empty arguments for optional parameters.

这篇关于C#编程方式创建和QUOT;命名范围&QUOT;在工作​​表范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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