如何使用 C# 在 Excel 中新增命名資料表
若要在 Excel 中使用 C# 新增命名資料表,請採用 IronXL 的 AddNamedTable 方法,並傳入資料表名稱、範圍及可選的樣式參數——透過單一方法呼叫即可實現結構化資料管理。
命名表格(named table)亦常被稱為 Excel 表格,指的是已指定名稱且具備相關附加功能與屬性的特定範圍類型。 命名資料表提供增強的資料組織功能、自動格式化、內建篩選功能,以及與 Excel 公式的無縫整合,使其成為在 Excel 自動化工作流程中管理結構化資料集不可或缺的工具。
快速入門:一行代碼建立並命名資料表
此範例展示如何輕鬆地在工作表中新增命名資料表:透過 IronXL,僅需一次清晰的方法呼叫,即可定義名稱、範圍、篩選可見性及樣式。
簡化工作流程(5 個步驟)
- 下載 C# 函式庫以新增命名資料表
- 使用 workSheet["A1:A5"] 選取目標範圍
- 使用
AddNamedTable方法新增命名資料表 - 透過多種方式檢索指定資料表
- 將編輯後的 Excel 檔案匯出為各種格式
如何將命名資料表新增至我的 Excel 試算表?
若要新增命名資料表,請使用 AddNamedTable 方法。 此方法需要以字串形式的資料表名稱以及範圍物件作為參數。 您亦可選擇指定表格樣式,以及是否顯示篩選器。 此功能在處理 DataSet 和 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(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");
Imports IronXL
Imports IronXl.Styles
' Load the Excel workbook
Dim workbook = WorkBook.Load("example.xlsx")
' Select the worksheet
Dim workSheet = workbook.WorkSheets.First()
' Define the range for the named table
Dim range = workSheet("A1:B10")
' Add a named table with the specified name and range
Dim 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")
命名表可透過 TableStyles 枚舉支援多種樣式選項。 您可以立即套用Professional格式,這與儲存格樣式和邊框等其他格式功能相輔相成。 以下是一個展示不同表格樣式應用的範例:
// 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");
Imports IronXL
Imports IronXl.Styles
Module Module1
Sub Main()
Dim workbook = WorkBook.Create()
Dim sheet = workbook.CreateWorkSheet("SalesData")
' Add sample data
sheet("A1").Value = "Product"
sheet("B1").Value = "Sales"
sheet("C1").Value = "Revenue"
' Populate data rows
For i As Integer = 2 To 10
sheet($"A{i}").Value = $"Product {i - 1}"
sheet($"B{i}").IntValue = i * 100
sheet($"C{i}").DecimalValue = i * 250.5D
Next
' Create a light-styled table
Dim 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"
Dim regionTable = sheet.AddNamedTable("RegionData", sheet("E1:F5"),
showFilter:=False,
tableStyle:=TableStyles.Dark3)
workbook.SaveAs("styled_tables.xlsx")
End Sub
End Module
如何從工作表中擷取命名資料表?
哪個方法會傳回工作表中所有命名資料表?
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
如何透過名稱存取特定的命名資料表?
請使用 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)
處理表格資料
命名表提供強大的資料操作功能。 以下是一個展示如何處理表格資料的完整範例:
// 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");
Imports IronXL
Imports System.Linq
Dim workbook = WorkBook.Load("sales_data.xlsx")
Dim sheet = workbook.DefaultWorkSheet
' Create a named table from existing data
Dim dataRange = sheet("A1:D20")
Dim salesTable = sheet.AddNamedTable("MonthlySales", dataRange, True)
' Access table data for calculations
Dim tableRange = salesTable.TableRange
' Sum values in a specific column (assuming column C contains numeric data)
Dim totalSales As Decimal = 0
For row As Integer = 2 To tableRange.RowCount
Dim cellValue = sheet($"C{row}").DecimalValue
totalSales += cellValue
Next
' Add summary row
Dim 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;
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");
Imports IronXL
Dim workbook = WorkBook.Create()
Dim 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 i As Integer = 2 To 6
sheet($"A{i}").Value = $"Item {i - 1}"
sheet($"B{i}").IntValue = i * 10
sheet($"C{i}").DecimalValue = i * 15.99D
' Add formula to calculate total
sheet($"D{i}").Formula = $"=B{i}*C{i}"
Next
' Create named table including the formula column
Dim 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")
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 及其他預定義表格樣式,以實現 Professional 格式設定。只需在建立表格時使用 SetStyle 方法,或指定 tableStyle 參數即可。
是否可以在命名資料表中顯示或隱藏篩選條件?
沒問題!IronXL 允許您控制命名資料表中篩選器的可見性。您可以將 ShowFilter 屬性設定為 true 或 false,或在建立資料表時,透過 AddNamedTable 方法中的 showFilter 參數直接指定。
建立命名資料表需要哪些參數?
IronXL 中的 AddNamedTable 方法需要兩個必填參數:表格名稱(字串型)以及定義表格區域的範圍物件。可選參數包括 showFilter(布林值)和 tableStyle(取自 TableStyles 枚舉)。
我可以在同一張工作表中建立多個具有樣式的命名資料表嗎?
是的,IronXL 允許您在同一工作表中建立多個具有不同樣式的命名表格。每個表格皆可擁有專屬的名稱、範圍、樣式及篩選設定,非常適合用於在單一 Excel 檔案中整理不同的資料集。

