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

如何使用 C# 在 Excel 中新增命名表

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

命名表也常被稱為 Excel 表,它指的是一種特定類型的區域,該區域已指定名稱,並具有與之關聯的附加功能和屬性。

快速入門:一行建立並命名表格

本範例展示如何使用 IronXL 在工作表中新增命名表—只需一次清晰的方法呼叫即可定義名稱、範圍、篩選可見度和樣式。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronXL

    PM > Install-Package IronXL.Excel

  2. 複製並運行這段程式碼。

    var table = workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle: IronXL.Styles.TableStyles.Medium2);
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronXL,免費試用!
    arrow pointer


新增命名表示例

若要新增命名表,請使用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
命名表

檢索命名表示例

檢索所有命名表

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("命名表: " + 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("命名表: " + name);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$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("命名表: " + 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("命名表: " + namedTable.Name);
Console.WriteLine("Rows: " + namedTable.Rows);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$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,738,553 | Version: 2025.11 剛發表