如何添加命名范围
命名范围是由唯一名称标识的指定单元格范围。 而不是通过单元格地址引用范围(如 A1:B10)您可以为范围指定一个名称,这样在公式和函数中引用和理解就更容易了。 例如,如果您将一个范围命名为“SalesData”,您可以在公式中像这样引用它:SUM(销售数据)而不是直接指定单元格区域。
如何添加命名范围
- 下载 C# 库以添加命名范围
- 选择目标范围 工作表 ["A1:A5"]
- 利用
添加命名范围
方法来添加命名范围 - 以各种方式检索命名范围
- 使用
删除已命名范围
方法
开始使用IronXL
立即在您的项目中开始使用IronXL,并享受免费试用。
添加命名范围示例
要添加一个命名范围,请使用 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")
检索命名范围示例
检索所有命名范围
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()
检索特定命名范围
使用 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}")
删除命名范围示例
要删除命名范围,请使用 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")