如何添加命名范围

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼特·宾

命名范围是由唯一名称标识的指定单元格范围。 而不是通过单元格地址引用范围(如 A1:B10)您可以为范围指定一个名称,这样在公式和函数中引用和理解就更容易了。 例如,如果您将一个范围命名为“SalesData”,您可以在公式中像这样引用它:SUM(销售数据)而不是直接指定单元格区域。


开始使用IronXL

立即在您的项目中开始使用IronXL,并享受免费试用。

第一步:
green arrow pointer


添加命名范围示例

要添加一个命名范围,请使用 AddNamedRange 方法,传递命名范围的名称作为文本和范围对象。

:path=/static-assets/excel/content-code-examples/how-to/named-range-add-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Select range
var selectedRange = workSheet["A1:A5"];

// Add named range
workSheet.AddNamedRange("range1", selectedRange);

workBook.SaveAs("addNamedRange.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Select range
Private selectedRange = workSheet("A1:A5")

' Add named range
workSheet.AddNamedRange("range1", selectedRange)

workBook.SaveAs("addNamedRange.xlsx")
VB   C#
命名范围

检索命名范围示例

检索所有命名范围

GetNamedRanges 方法将返回工作表中所有命名范围的字符串列表。

:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-all-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Get all named range
var namedRangeList = workSheet.GetNamedRanges();
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Get all named range
Private namedRangeList = workSheet.GetNamedRanges()
VB   C#

检索特定命名范围

使用 FindNamedRange 方法来检索命名范围的绝对引用,例如 Sheet1。!$A$1:$A$5。地址公式可用于引用命名范围或选择与命名范围对应的范围。 在选择范围时,您应该注意工作表名称。

:path=/static-assets/excel/content-code-examples/how-to/named-range-retrieve-specific-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Get named range address
string namedRangeAddress = workSheet.FindNamedRange("range1");

// Select range
var range = workSheet[$"{namedRangeAddress}"];
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Get named range address
Private namedRangeAddress As String = workSheet.FindNamedRange("range1")

' Select range
Private range = workSheet($"{namedRangeAddress}")
VB   C#

删除命名范围示例

要删除命名范围,请使用 RemoveNamedRange 方法并传递命名范围名称作为文本。

:path=/static-assets/excel/content-code-examples/how-to/named-range-remove-named-range.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addNamedRange.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove named range
workSheet.RemoveNamedRange("range1");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addNamedRange.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Remove named range
workSheet.RemoveNamedRange("range1")
VB   C#
Chaknith related to 删除命名范围示例

查克尼特·宾

软件工程师

Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。