IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月4日

要在Excel中使用C#新增命名表格,請使用AddNamedTable方法,設置表格名稱、範圍及選擇性樣式,透過單一方法呼叫實現結構化資料管理。

命名表格也常被稱為Excel表格,指的是特定型別的範圍,其經過命名並有額外的功能和屬性。 命名表格提供增強的資料組織能力、自動格式化、內建篩選及與Excel公式的無縫整合,是管理結構化資料集在Excel自動化工作流程中的必要工具。

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

此範例展示如何輕鬆地在工作表中使用IronXL新增命名表格—定義名稱、範圍、篩選可見性及樣式,均在單一明確的方法呼叫中完成。

  1. 1Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

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

    var table = workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle: IronXL.Styles.TableStyles.Medium2);
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronXL,透過免費試用
    arrow pointer

如何將命名表格新增至我的Excel工作表?

要新增命名表格,請使用AddNamedTable方法。 此方法需要表格名稱作為字串及範圍物件。 您也可以選擇指定表格樣式及是否顯示篩選功能。 此功能在處理DataTable導入需要妥善組織結構化資料時特別有用。

// 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(TableStyle.TableStyleDark10);
namedTable.ShowFilter = true;

// Save the modified workbook
workbook.SaveAs("modified_example.xlsx");
C#

命名表格透過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: TableStyle.TableStyleLight15);

// 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: TableStyle.TableStyleDark3);

workbook.SaveAs("styled_tables.xlsx");
C#
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);
}

我如何根據名稱存取特定命名表格?

使用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);

處理表格資料

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

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

與其他IronXL功能的整合

命名表格可以與其他IronXL功能無縫配合使用。 您可以結合公式來進行動態計算,或在建立圖表時用作資料來源。 它們在匯出到不同格式之前也是整理資料的極佳選擇。

// Example: Named table with formulas
using IronXL;
using IronXL.Styles;

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: TableStyle.TableStyleMedium9);

// 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");
C#

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檔案中組織不同的資料集。

How do named tables in IronXL enhance data manipulation capabilities?

Named tables in IronXL offer powerful data manipulation capabilities. They allow for advanced operations like summing column values programmatically, adding summary rows, and applying conditional formatting, making them valuable for comprehensive Excel data management.

Can I integrate named tables with other features in IronXL?

Yes, named tables seamlessly integrate with other IronXL features. You can use them in conjunction with formulas for dynamic calculations, as data sources for creating charts, or as organized data structures before exporting to different formats.

How do I include formulas within a named table using IronXL?

When adding a named table with IronXL, you can include columns with formulas. For instance, you can calculate totals for each row based on other column values and then add grand total formulas below the data rows for comprehensive data analysis.

Where can I learn more about adding named ranges using IronXL?

You can learn more about adding named ranges in IronXL by visiting their documentation page on 'How to Add Named Range'. This section offers insights into creating and managing named ranges within Excel workbooks programmatically.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備好開始了嗎?

Nuget Downloads 2,237,574版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的免費30天試用金鑰
無需信用卡或帳戶建立
PDF的C# NuGet程式庫
NuGet安裝

版本: 2026.9

PM > Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
  1. 在方案資源管理器中,右鍵單擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋“IronXL”
  3. 選擇包並安裝
C# PDF DLL
下載DLL

版本: 2026.9

  1. 下載並解壓IronXL到您的方案目錄中的比如~/Libs位置
  2. 在Visual Studio方案資源管理器中,右鍵單擊參考。選擇瀏覽,“IronXL.dll”

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲得無義務諮詢
填寫以下表格或電郵sales@ironsoftware.com
您的資料將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立