Wie man eine benannte Tabelle in Excel mit C# hinzufügt | IronXL

Hinzufügen einer benannten Tabelle in Excel mit C#35;

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

Um eine benannte Tabelle in Excel mit C# hinzuzufügen, verwenden Sie die IronXL-Methode AddNamedTable mit Parametern für Tabellenname, Bereich und optionales Styling, die eine strukturierte Datenverwaltung mit einem einzigen Methodenaufruf ermöglicht.

Eine benannte Tabelle wird auch oft als Excel-Tabelle bezeichnet und bezieht sich auf einen speziellen Arten-Ereignis, der mit einem Namen versehen wurde und zusätzliche Funktionen und Eigenschaften enthält. Benannte Tabellen bieten erweiterte Möglichkeiten der Datenorganisation, automatische Formatierung, integrierte Filterung und nahtlose Integration mit Excel-Formeln - und sind damit unverzichtbar für die Verwaltung strukturierter Datensätze in Excel-Automatisierungsworkflows.

Schnellstart: Erstellen und Benennen einer Tabelle in einer Zeile

Dieses Beispiel zeigt, wie mühelos Sie eine benannte Tabelle in Ihrem Arbeitsblatt mit IronXL hinzufügen können—definieren Sie den Namen, den Bereich, die Sichtbarkeit des Filters und den Stil in einem einzigen klaren Methodenaufruf.

Nuget IconLegen Sie jetzt mit NuGet los, um PDFs zu erstellen:

  1. Installieren Sie IronXL mit dem NuGet-Paketmanager.

    PM > Install-Package IronXL.Excel

  2. Kopieren Sie diesen Codeausschnitt und führen Sie ihn aus.

    var table = workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle: IronXL.Styles.TableStyles.Medium2);
  3. Bereitstellen zum Testen in Ihrer Live-Umgebung

    Beginnen Sie noch heute mit der Nutzung von IronXL in Ihrem Projekt – mit einer kostenlosen Testversion.
    arrow pointer


Wie füge ich eine benannte Tabelle zu meinem Excel-Arbeitsblatt hinzu?

Um eine benannte Tabelle hinzuzufügen, verwenden Sie die AddNamedTable-Methode. Die Methode erfordert den Namen der Tabelle als Zeichenfolge und das Bereichsobjekt. Sie haben auch die Möglichkeit, den Tabellenstil zu bestimmen und anzugeben, ob der Filter angezeigt werden soll. Diese Funktionalität ist besonders nützlich bei der Arbeit mit DataSet- und DataTable-Importen, bei denen strukturierte Daten richtig organisiert werden müssen.

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

Benannte Tabellen unterstützen verschiedene Styling-Optionen durch die TableStyles Aufzählung. Sie können sofort eine professionelle Formatierung anwenden, die andere Formatierungsfunktionen wie Zellengestaltung und Rahmen ergänzt. Hier ist ein Beispiel, das verschiedene Anwendungen im Tabellenstil zeigt:

// 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Excel-Tabelle, die eine benannte Tabelle mit drei Spalten und formatierten Überschriften zeigt, die Beispieltextdaten enthält

Wie kann ich benannte Tabellen aus meinem Arbeitsblatt abrufen?

Welche Methode liefert alle benannten Tabellen in einem Arbeitsblatt?

Die Methode GetNamedTableNames gibt alle benannten Tabellen im Arbeitsblatt als eine Liste von Strings zurück. Dies ist besonders nützlich bei der Arbeit mit Arbeitsmappen, die mehrere Tabellen enthalten, oder bei der Verwaltung von Arbeitsblättern mit dynamischen Datenstrukturen.

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

Wie greife ich auf eine bestimmte benannte Tabelle über ihren Namen zu?

Verwenden Sie die GetNamedTable-Methode, um eine bestimmte benannte Tabelle im Arbeitsblatt abzurufen. Nach dem Abruf können Sie auf verschiedene Eigenschaften zugreifen und Operationen wie Sortieren von Zellbereichen oder die Anwendung von conditional formatting durchführen.

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

Arbeiten mit Tabellendaten

Benannte Tabellen bieten leistungsstarke Funktionen zur Datenmanipulation. Hier ist ein umfassendes Beispiel, das zeigt, wie man mit Tabellendaten arbeitet:

// 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Integration mit anderen IronXL-Funktionen

Benannte Tabellen arbeiten nahtlos mit anderen IronXL-Funktionen zusammen. Sie können sie mit Formeln für dynamische Berechnungen kombinieren oder sie als Datenquellen bei der Erstellung von Diagrammen verwenden. Sie eignen sich auch hervorragend zum Organisieren von Daten vor dem Export in verschiedene Formate.

// 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

IronXL kann auch benannte Bereiche hinzufügen. Erfahren Sie mehr unter Wie man einen benannten Bereich hinzufügt.

Häufig gestellte Fragen

Was ist eine benannte Tabelle in Excel?

Eine benannte Tabelle in Excel ist eine bestimmte Art von Bereich, der mit einem Namen versehen wurde und zusätzliche Funktionen enthält. IronXL ermöglicht es Ihnen, diese Tabellen programmatisch in C# zu erstellen, und bietet erweiterte Möglichkeiten der Datenorganisation, automatische Formatierung, integrierte Filterung und nahtlose Integration mit Excel-Formeln.

Wie füge ich mit C# eine benannte Tabelle zu einem Excel-Arbeitsblatt hinzu?

Um eine benannte Tabelle mit IronXL hinzuzufügen, verwenden Sie die Methode AddNamedTable. Diese Methode erfordert den Tabellennamen als String und ein Range-Objekt. Sie können optional den Tabellenstil und die Sichtbarkeit des Filters angeben. Zum Beispiel: workSheet.AddNamedTable("MyTable", workSheet.GetRange("A1:B5"), showFilter: true, tableStyle: IronXL.Styles.TableStyles.Medium2).

Kann ich benannten Tabellen ein eigenes Styling geben?

Ja, IronXL unterstützt verschiedene Styling-Optionen für benannte Tabellen durch die Aufzählung TableStyles. Mit Stilen wie Dark10, Medium2 und anderen vordefinierten Tabellenstilen können Sie sofort eine professionelle Formatierung anwenden. Verwenden Sie einfach die SetStyle-Methode oder geben Sie den tableStyle-Parameter bei der Erstellung der Tabelle an.

Ist es möglich, Filter in benannten Tabellen ein- oder auszublenden?

Unbedingt! IronXL ermöglicht es Ihnen, die Sichtbarkeit von Filtern in benannten Tabellen zu steuern. Sie können die Eigenschaft ShowFilter auf true oder false setzen oder sie direkt beim Erstellen der Tabelle mit dem showFilter Parameter in der AddNamedTable Methode angeben.

Was sind die erforderlichen Parameter für die Erstellung einer benannten Tabelle?

Die AddNamedTable-Methode in IronXL erfordert zwei wesentliche Parameter: den Tabellennamen (als String) und das Bereichsobjekt, das den Tabellenbereich definiert. Optionale Parameter sind showFilter (boolesch) und tableStyle (aus der Aufzählung TableStyles).

Kann ich in einem Arbeitsblatt mehrere benannte Tabellen mit Stil erstellen?

Ja, IronXL ermöglicht es Ihnen, mehrere benannte Tabellen mit unterschiedlichen Stilen im selben Arbeitsblatt zu erstellen. Jede Tabelle kann einen eigenen Namen, einen eigenen Bereich, eine eigene Formatvorlage und eigene Filtereinstellungen haben und eignet sich somit perfekt für die Organisation verschiedener Datensätze in einer einzigen Excel-Datei.

Chaknith Bin
Software Ingenieur
Chaknith arbeitet an IronXL und IronBarcode. Er hat umfassende Expertise in C# und .NET und hilft, die Software zu verbessern und Kunden zu unterstützen. Seine Einblicke aus Benutzerinteraktionen tragen zu besseren Produkten, Dokumentationen und einem insgesamt besseren Erlebnis bei.
Bereit anzufangen?
Nuget Downloads 1,765,830 | Version: 2025.12 gerade veröffentlicht