如何使用 C# 添加 Excel 命名表 | IronXL

How to Add Named Table

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

A named table is also commonly known as an Excel Table, which refers to a specific type of range that has been designated with a name and has additional functionality and properties associated with it.

Quickstart: Create and Name a Table in One Line

This example shows how effortlessly you can add a named table in your worksheet using IronXL—define the name, range, filter visibility, and style all in a single clear method call.

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


Add Named Table Example

To add a named table, use the AddNamedTable method. The method requires the name of the table as a string and the range object. The user also has the option to specify the table style and whether to show the filter or not.

// 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
Named Table

Retrieve Named Table Example

Retrieve All Named Tables

The GetNamedTableNames method will return all named tables in the worksheet as a list of strings.

// 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

Retrieve Specific Named Table

Use the GetNamedTable method to retrieve a specific named table in the worksheet.

// 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 can also add named ranges. Learn more at How to Add Named Range.

常见问题解答

如何在 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 中的命名表有什么优势?

Excel 中的命名表,由 IronXL 提供支持,提供组织化的数据管理、增强的可读性以及对特定数据范围的轻松访问,从而提高数据分析和操作效率。

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

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

IronXL 是否支持除命名表之外的命名范围的添加?

是的,IronXL 支持命名表和命名范围,允许在 Excel 电子表格中实现灵活的数据组织和访问。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布