如何新增命名範圍
指定範圍是由唯一名稱標識的一組特定單元格。 與其透過儲存格地址(如 A1:B10)參考一個範圍,不如給該範圍指定一個名稱,這樣在公式和函數中引用和理解會更容易。 例如,如果您將範圍命名為 "SalesData",您可以在公式中像使用 SUM(SalesData) 這樣引用它,而不是直接指定儲存格範圍。
如何新增命名範圍
- 下載 C# 庫以添加命名範圍
- 選擇目標範圍使用 workSheet ["A1:A5"]
- 利用
AddNamedRange
方法添加命名範圍 - 以多種方式檢索命名範圍
- 輕鬆使用
RemoveNamedRange
方法移除命名範圍
開始使用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")