如何添加命名范围
命名单元格区域是由唯一名称标识的指定单元格区域。而不是通过单元格地址来引用单元格区域 (如 A1:B10)您可以为范围指定名称,使其在公式和函数中更易于引用和理解。例如,如果将范围命名为 "SalesData",就可以在 SUM 这样的公式中引用它(销售数据) 而不是直接指定单元格区域。
如何添加命名范围
- 下载 C# 库以添加命名范围
- 选择目标范围 工作表 ["A1:A5"]
- 利用
添加命名范围
方法来添加命名范围 - 以各种方式检索命名范围
- 使用
删除已命名范围
方法
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronXL 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变Excel。
Install-Package IronXL.Excel
考虑安装 IronXL DLL 直接。下载并手动安装到您的项目或GAC表单中: IronXL.zip
手动安装到你的项目中
下载DLL添加命名范围示例
要添加已命名的范围,请使用 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!然后就可以使用地址公式引用已命名的范围或选择与已命名范围相对应的范围。选择范围时,应注意工作表名称。
: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")