C#を使用してExcelで名前付きテーブルを追加する方法 | IronXL

C#を使用してExcelに名前付きテーブルを追加する方法</#35;

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

C#を使用してExcelに名前付きテーブルを追加するには、IronXLのAddNamedTableメソッドを使用し、テーブル名、範囲、オプションのスタイルをパラメータとして指定します。

名前付きテーブルは、Excelテーブルとも呼ばれることがあり、名前が付けられた特定の種類の範囲を指し、それに関連する追加の機能やプロパティがあります。 名前付きテーブルは、強化されたデータ整理機能、自動書式設定、組み込みフィルタリング、Excel 数式とのシームレスな統合を提供し、Excel 自動化ワークフローで構造化データセットを管理するために不可欠です。

クイックスタート:1行でテーブルを作成し、名前を付ける

この例では、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


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

名前付きテーブルは、TableStyles列挙を通じて、さまざまなスタイリング・オプションをサポートしています。 cell styling and bordersのような他の書式設定機能を補完する、プロフェッショナルな書式設定を即座に適用することができます。 以下は、さまざまなテーブルスタイルのアプリケーションを示す例です:

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

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")
$vbLabelText   $csharpLabel
Excelのスプレッドシートに、サンプルテキストデータを含む3つの列とフォーマットされたヘッダーを持つ名前付きテーブルを表示.

ワークシートから名前付きテーブルを取得するには?

ワークシート内のすべての名前付きテーブルを返すメソッドは何ですか?

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
$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);
' 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

テーブルデータを扱う

名前付きテーブルは、強力なデータ操作機能を提供します。 テーブルデータの扱い方を示す包括的な例です:

// 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")
$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");
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")
$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メソッドは、テーブル名(文字列)とテーブル領域を定義する範囲オブジェクトの2つの必須パラメータを必要とします。オプションのパラメータには showFilter (boolean) と tableStyle (TableStyles 列挙) があります。

同じワークシートに複数のスタイル付き名前付きテーブルを作成できますか?

はい、IronXLでは同じワークシートに異なるスタイルの複数の名前付きテーブルを作成することができます。各テーブルは独自の名前、範囲、スタイル、フィルター設定を持つことができ、1つのExcelファイル内で異なるデータセットを整理するのに最適です。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 1,846,091 | バージョン: 2026.2 リリース