c#:以编程方式创建“命名范围”在工作表范围 [英] c#: programmatically create a "named range" in worksheet scope

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

问题描述

如何创建一个范围,其范围设置为工作表? (我们可以从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)

使用'范围设置名称。名称'属性创建一个工作簿范围的命名范围。我尝试使用此处建议的< Sheet Name>!前缀范围名称,但不起作用。这个名字没有这样设置。

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/05/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

修改后的代码是:

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#:以编程方式创建“命名范围”在工作表范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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