如何新增命名範圍
命名範圍是由唯一名稱標識的特定單元格範圍。與其通過單元格地址引用範圍 (如 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!$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")