如何在 C# 中添加命名表 | IronXL

How to Add Named Table

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

命名表格也通常被稱為 Excel 表格,指的是一種特定類型的範圍,該範圍已被指定名稱並具有與之相關的附加功能和屬性。

快速入門:在一行中創建並命名表格

此範例展示了如何輕鬆地在工作表中添加命名表格—定義名稱、範圍、篩選可見性和樣式,全部在一個清晰的方法調用中完成。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var table = workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle: IronXL.Styles.TableStyles.Medium2);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最低工作流程(5 步驟)

  1. 下載 C# 庫以添加命名表格
  2. 使用 workSheet["A1:A5"] 選擇目標範圍
  3. 利用 AddNamedTable 方法添加命名表格
  4. 以各種方式檢索命名表格
  5. 以各種格式導出已編輯的 Excel 文件


添加命名表格範例

要添加命名表格,請使用 AddNamedTable 方法。 該方法需要表格的名稱作為字符串以及範圍對象。 用戶還可以選擇指定表格樣式以及是否顯示篩選功能。

// Example code to add a named table using IronXL
using IronXL;
using IronXL.Styles;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Define the range for the named table
var range = workSheet["A1:B10"];

// Add a named table with the specified name and range
var namedTable = workSheet.AddNamedTable("MyTable", range);

// Optionally, set table style and visibility of the filter
namedTable.SetStyle(TableStyles.Dark10);
namedTable.ShowFilter = true;

// Save the modified workbook
workbook.SaveAs("modified_example.xlsx");
// Example code to add a named table using IronXL
using IronXL;
using IronXL.Styles;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Define the range for the named table
var range = workSheet["A1:B10"];

// Add a named table with the specified name and range
var namedTable = workSheet.AddNamedTable("MyTable", range);

// Optionally, set table style and visibility of the filter
namedTable.SetStyle(TableStyles.Dark10);
namedTable.ShowFilter = true;

// Save the modified workbook
workbook.SaveAs("modified_example.xlsx");
' Example code to add a named table using IronXL
Imports IronXL
Imports IronXL.Styles

' Load the Excel workbook
Private workbook = WorkBook.Load("example.xlsx")
' Select the worksheet
Private workSheet = workbook.WorkSheets.First()

' Define the range for the named table
Private range = workSheet("A1:B10")

' Add a named table with the specified name and range
Private namedTable = workSheet.AddNamedTable("MyTable", range)

' Optionally, set table style and visibility of the filter
namedTable.SetStyle(TableStyles.Dark10)
namedTable.ShowFilter = True

' Save the modified workbook
workbook.SaveAs("modified_example.xlsx")
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> Named Table

檢索命名表格範例

檢索所有命名表格

GetNamedTableNames 方法將返回工作表中所有命名表格作為字符串列表。

// Example code to retrieve all named table names using IronXL
using IronXL;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Retrieve all named table names
var tableNames = workSheet.GetNamedTableNames();

// Output each table name
foreach (var name in tableNames)
{
    Console.WriteLine("Named Table: " + name);
}
// Example code to retrieve all named table names using IronXL
using IronXL;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Retrieve all named table names
var tableNames = workSheet.GetNamedTableNames();

// Output each table name
foreach (var name in tableNames)
{
    Console.WriteLine("Named Table: " + name);
}
' Example code to retrieve all named table names using IronXL
Imports IronXL

' Load the Excel workbook
Private workbook = WorkBook.Load("example.xlsx")
' Select the worksheet
Private workSheet = workbook.WorkSheets.First()

' Retrieve all named table names
Private tableNames = workSheet.GetNamedTableNames()

' Output each table name
For Each name In tableNames
	Console.WriteLine("Named Table: " & name)
Next name
$vbLabelText   $csharpLabel

檢索特定的命名表格

使用 GetNamedTable 方法來檢索工作表中的特定命名表格。

// Example code to retrieve a specific named table using IronXL
using IronXL;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Retrieve a specific named table
var namedTable = workSheet.GetNamedTable("MyTable");

// Output some information about the table
Console.WriteLine("Named Table: " + namedTable.Name);
Console.WriteLine("Rows: " + namedTable.Rows);
// Example code to retrieve a specific named table using IronXL
using IronXL;

// Load the Excel workbook
var workbook = WorkBook.Load("example.xlsx");
// Select the worksheet
var workSheet = workbook.WorkSheets.First();

// Retrieve a specific named table
var namedTable = workSheet.GetNamedTable("MyTable");

// Output some information about the table
Console.WriteLine("Named Table: " + namedTable.Name);
Console.WriteLine("Rows: " + namedTable.Rows);
' Example code to retrieve a specific named table using IronXL
Imports IronXL

' Load the Excel workbook
Private workbook = WorkBook.Load("example.xlsx")
' Select the worksheet
Private workSheet = workbook.WorkSheets.First()

' Retrieve a specific named table
Private namedTable = workSheet.GetNamedTable("MyTable")

' Output some information about the table
Console.WriteLine("Named Table: " & namedTable.Name)
Console.WriteLine("Rows: " & namedTable.Rows)
$vbLabelText   $csharpLabel

IronXL 還可以添加命名範圍。 在如何添加命名範圍了解更多。

常見問題解答

如何在 C# 的 Excel 工作表中添加命名表?

要在 C# 中添加命名表,请使用 IronXL 的 AddNamedTable 方法。首先,从 NuGet 下载 IronXL 库。加载您的 Excel 工作簿,选择工作表,定义范围,然后在 AddNamedTable 方法中指定表名。您还可以设置表格样式和过滤器可见性。

如何在 IronXL 中为命名表格设置样式?

在 IronXL 中,使用 AddNamedTable 方法添加命名表格后,可以使用 SetStyle 方法为其设置样式。从 TableStyles 中选择样式,例如 TableStyles.Dark10 以应用所需的外观。

如何使用 IronXL 从 Excel 工作表中检索所有命名表?

使用 IronXL 的 GetNamedTableNames 方法列出工作表中的所有命名表。此方法返回一个字符串列表,每个字符串代表 Excel 工作表中的一个命名表。

如何在 C# 中检索特定的命名表?

要在 IronXL 中检索特定命名表,请使用带有表名的 GetNamedTable 方法。这样您就可以访问命名表及其属性,以便进一步操作。

我可以使用 IronXL 将编辑过的 Excel 文件导出为不同格式吗?

可以,IronXL 允许您将编辑过的 Excel 文件导出为各种格式,确保与不同应用程序和系统的兼容性。

使用 Excel 命名表的优势是什么?

由 IronXL 提供支持的 Excel 命名表提供组织化的数据管理,加强可读性,并能轻松访问特定的数据范围,提高数据分析和操作效率。

如何使用 IronXL 控制命名表中的过滤器可见性?

在 IronXL 中,可以通过设置 ShowFilter 属性来控制命名表中过滤器的可见性。将其设置为 true 以显示过滤器,或设置为 false 以隐藏过滤器。

IronXL 除了命名表外,还支持添加命名范围吗?

可以,IronXL 支持命名表和命名范围,使 Excel 电子表格中的数据组织和访问更灵活。

Chaknith Bin
軟體工程師
Chaknith 在 IronXL 和 IronBarcode 上工作。他對 C# 和 .NET 擁有深厚的專業知識,幫助改進了軟體並支持客戶。他從用戶互動中得到的見解有助於改善產品、文檔和整體體驗。
準備好開始了嗎?
Nuget 下載 1,686,155 | 版本: 2025.11 剛剛發布