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

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

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

若要使用 C# 在 Excel 中新增命名表,請使用 IronXL 的 AddNamedTable 方法,並傳入表名、範圍和可選樣式等參數——只需一次方法呼叫即可實現結構化資料管理。

命名表也常被稱為 Excel 表,它指的是一種特定類型的區域,該區域已指定名稱,並具有與之關聯的附加功能和屬性。 命名表提供增強的資料組織功能、自動格式化、內建篩選以及與 Excel 公式的無縫集成,使其成為在Excel 自動化工作流程中管理結構化資料集的必要工具。

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

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

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronXL.Excel

    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


如何在Excel工作表中新增已命名的表格?

若要新增命名表,請使用 AddNamedTable 方法。 此方法需要以字串形式提供表名,以及範圍物件。 您也可以選擇指定表格樣式以及是否顯示篩選器。 當處理DataSetDataTable 導入時,此功能尤其有用,因為結構化資料需要適當的組織。

// 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");
$vbLabelText   $csharpLabel

命名表透過 TableStyles 枚舉支援各種樣式選項。 您可以立即套用專業格式,這與其他格式設定功能(如儲存格樣式和邊框)相輔相成。 以下範例展示了不同表格樣式的應用:

// Example: Creating multiple styled named tables
using IronXL;
using IronXl.Styles;

var workbook = WorkBook.Create();
var sheet = workbook.CreateWorkSheet("SalesData");

// Add sample data
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Sales";
sheet["C1"].Value = "Revenue";

// Populate data rows
for (int i = 2; i <= 10; i++)
{
    sheet[$"A{i}"].Value = $"Product {i-1}";
    sheet[$"B{i}"].IntValue = i * 100;
    sheet[$"C{i}"].DecimalValue = i * 250.50m;
}

// Create a light-styled table
var salesTable = sheet.AddNamedTable("SalesTable", sheet["A1:C10"], 
    showFilter: true, 
    tableStyle: TableStyles.Light15);

// Create another table with dark styling
sheet["E1"].Value = "Region";
sheet["F1"].Value = "Performance";
var regionTable = sheet.AddNamedTable("RegionData", sheet["E1:F5"], 
    showFilter: false, 
    tableStyle: TableStyles.Dark3);

workbook.SaveAs("styled_tables.xlsx");
// Example: Creating multiple styled named tables
using IronXL;
using IronXl.Styles;

var workbook = WorkBook.Create();
var sheet = workbook.CreateWorkSheet("SalesData");

// Add sample data
sheet["A1"].Value = "Product";
sheet["B1"].Value = "Sales";
sheet["C1"].Value = "Revenue";

// Populate data rows
for (int i = 2; i <= 10; i++)
{
    sheet[$"A{i}"].Value = $"Product {i-1}";
    sheet[$"B{i}"].IntValue = i * 100;
    sheet[$"C{i}"].DecimalValue = i * 250.50m;
}

// Create a light-styled table
var salesTable = sheet.AddNamedTable("SalesTable", sheet["A1:C10"], 
    showFilter: true, 
    tableStyle: TableStyles.Light15);

// Create another table with dark styling
sheet["E1"].Value = "Region";
sheet["F1"].Value = "Performance";
var regionTable = sheet.AddNamedTable("RegionData", sheet["E1:F5"], 
    showFilter: false, 
    tableStyle: TableStyles.Dark3);

workbook.SaveAs("styled_tables.xlsx");
$vbLabelText   $csharpLabel
Excel電子表格顯示了一個命名表,該表包含三個列和格式化的表頭,表頭中包含範例文字資料。

如何從工作表中檢索指定名稱的表格?

哪個方法可以傳回工作表中所有已命名的表?

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);
}
$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);
$vbLabelText   $csharpLabel

處理表格數據

命名表提供了強大的資料操作功能。 以下是一個全面的範例,展示如何處理表格資料:

// Advanced named table operations
using IronXL;
using System.Linq;

var workbook = WorkBook.Load("sales_data.xlsx");
var sheet = workbook.DefaultWorkSheet;

// Create a named table from existing data
var dataRange = sheet["A1:D20"];
var salesTable = sheet.AddNamedTable("MonthlySales", dataRange, true);

// Access table data for calculations
var tableRange = salesTable.TableRange;

// Sum values in a specific column (assuming column C contains numeric data)
decimal totalSales = 0;
for (int row = 2; row <= tableRange.RowCount; row++)
{
    var cellValue = sheet[$"C{row}"].DecimalValue;
    totalSales += cellValue;
}

// Add summary row
var summaryRow = tableRange.RowCount + 1;
sheet[$"B{summaryRow}"].Value = "Total:";
sheet[$"C{summaryRow}"].Value = totalSales;

// Apply formatting to the summary row
sheet[$"B{summaryRow}:D{summaryRow}"].Style.Font.Bold = true;
sheet[$"B{summaryRow}:D{summaryRow}"].Style.SetBackgroundColor("#FFE599");

workbook.SaveAs("sales_with_summary.xlsx");
// Advanced named table operations
using IronXL;
using System.Linq;

var workbook = WorkBook.Load("sales_data.xlsx");
var sheet = workbook.DefaultWorkSheet;

// Create a named table from existing data
var dataRange = sheet["A1:D20"];
var salesTable = sheet.AddNamedTable("MonthlySales", dataRange, true);

// Access table data for calculations
var tableRange = salesTable.TableRange;

// Sum values in a specific column (assuming column C contains numeric data)
decimal totalSales = 0;
for (int row = 2; row <= tableRange.RowCount; row++)
{
    var cellValue = sheet[$"C{row}"].DecimalValue;
    totalSales += cellValue;
}

// Add summary row
var summaryRow = tableRange.RowCount + 1;
sheet[$"B{summaryRow}"].Value = "Total:";
sheet[$"C{summaryRow}"].Value = totalSales;

// Apply formatting to the summary row
sheet[$"B{summaryRow}:D{summaryRow}"].Style.Font.Bold = true;
sheet[$"B{summaryRow}:D{summaryRow}"].Style.SetBackgroundColor("#FFE599");

workbook.SaveAs("sales_with_summary.xlsx");
$vbLabelText   $csharpLabel

與其他IronXL功能的集成

命名表可以與其他IronXL功能無縫協作。 您可以將它們與公式結合使用進行動態計算,或者在建立圖表時將它們用作資料來源。 它們也非常適合在匯出為不同格式之前整理資料。

// Example: Named table with formulas
using IronXL;

var workbook = WorkBook.Create();
var sheet = workbook.CreateWorkSheet("Analysis");

// Create data structure
sheet["A1"].Value = "Item";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Price";
sheet["D1"].Value = "Total";

// Add sample data
for (int i = 2; i <= 6; i++)
{
    sheet[$"A{i}"].Value = $"Item {i-1}";
    sheet[$"B{i}"].IntValue = i * 10;
    sheet[$"C{i}"].DecimalValue = i * 15.99m;
    // Add formula to calculate total
    sheet[$"D{i}"].Formula = $"=B{i}*C{i}";
}

// Create named table including the formula column
var priceTable = sheet.AddNamedTable("PriceCalculations", sheet["A1:D6"], 
    showFilter: true, 
    tableStyle: TableStyles.Medium9);

// Add a grand total formula
sheet["C7"].Value = "Grand Total:";
sheet["D7"].Formula = "=SUM(D2:D6)";
sheet["D7"].Style.Font.Bold = true;

workbook.SaveAs("table_with_formulas.xlsx");
// Example: Named table with formulas
using IronXL;

var workbook = WorkBook.Create();
var sheet = workbook.CreateWorkSheet("Analysis");

// Create data structure
sheet["A1"].Value = "Item";
sheet["B1"].Value = "Quantity";
sheet["C1"].Value = "Price";
sheet["D1"].Value = "Total";

// Add sample data
for (int i = 2; i <= 6; i++)
{
    sheet[$"A{i}"].Value = $"Item {i-1}";
    sheet[$"B{i}"].IntValue = i * 10;
    sheet[$"C{i}"].DecimalValue = i * 15.99m;
    // Add formula to calculate total
    sheet[$"D{i}"].Formula = $"=B{i}*C{i}";
}

// Create named table including the formula column
var priceTable = sheet.AddNamedTable("PriceCalculations", sheet["A1:D6"], 
    showFilter: true, 
    tableStyle: TableStyles.Medium9);

// Add a grand total formula
sheet["C7"].Value = "Grand Total:";
sheet["D7"].Formula = "=SUM(D2:D6)";
sheet["D7"].Style.Font.Bold = true;

workbook.SaveAs("table_with_formulas.xlsx");
$vbLabelText   $csharpLabel

IronXL也可以新增命名範圍。 了解更多信息,請參閱"如何新增命名範圍"

常見問題解答

什麼是 Excel 中的命名表格?

Excel 中的命名表格是一種特定類型的範圍,已指定名稱,並包含附加功能。IronXL 可讓您以 C# 程式化的方式建立這些表格,提供增強的資料組織功能、自動格式化、內建篩選功能,以及與 Excel 公式的無縫整合。

如何使用 C# 在 Excel 工作表中加入已命名的表格?

若要使用 IronXL 新增已命名的表格,請使用 AddNamedTable 方法。此方法需要以字串形式提供表名和範圍物件。您可以選擇性地指定表格樣式和篩選器的可見性。例如:workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle:IronXl.Styles.TableStyles.Medium2).

我可以為已命名的表格套用自訂樣式嗎?

是的,IronXL 透過 TableStyles 枚舉支援已命名表格的各種樣式選項。您可以使用 Dark10、Medium2 等樣式以及其他預先定義的表格樣式,立即套用專業格式。只需使用 SetStyle 方法或在建立表格時指定 tableStyle 參數。

是否可以在已命名的表格中顯示或隱藏篩選器?

絕對可以IronXL 允許您在已命名的表格中控制篩選器的可見性。您可以設定 ShowFilter 屬性為 true 或 false,或在建立表格時使用 AddNamedTable 方法中的 showFilter 參數直接指定。

建立命名表所需的參數為何?

IronXL 中的 AddNamedTable 方法需要兩個基本參數:表格名稱(以字串形式)和定義表格區域的範圍物件。可選參數包括 showFilter (布林) 和 tableStyle (來自 TableStyles 枚舉)。

我可以在同一個工作表中建立多個有樣式命名的表格嗎?

是的,IronXL 允許您在同一個工作表中建立多個具有不同樣式的命名表格。每個表格都可以有自己獨特的名稱、範圍、樣式和篩選設定,非常適合在單一 Excel 檔案中組織不同的資料集。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,913,565 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronXL.Excel
執行範例 觀看您的資料變成試算表。