IronWordを使ってC#でDOCXにテーブルを追加する方法
IronWordを使うと、C#のコードからWord文書にテーブルを追加できます。行数と列数を指定してTableオブジェクトを作成し、罫線や色を設定してセルに内容を入力した後、DOCXファイルとして保存します。
この例では、IronWordでテーブルを作成する方法を示します。行数と列数を指定してテーブルを作成し、スタイルを適用して内容を入力したら、文書に挿入して保存します。スタイルを設定したテーブルを含むDOCXファイルを数分で作成できます。
-
1IronWord を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
var table = new IronWord.Models.Table(3,4); var doc = new IronWord.WordDocument(); doc.AddTable(table); doc.SaveAs("QuickTable.docx");Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx")Dim table As New IronWord.Models.Table(3, 4) Dim doc As New IronWord.WordDocument() doc.AddTable(table) doc.SaveAs("QuickTable.docx") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronWordを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- DOCXにテーブルを追加するためのC#ライブラリをダウンロードする
- セルに内容を入力し、セルをまとめて行を構成する
- 行を追加してテーブルを作成する
- テーブルを使って新しいWord文書を初期化し、テーブルを追加する
- 最終的なWord文書をエクスポートする
Word文書にテーブルを追加する方法
テーブルはWord文書の基本的な構成要素です。まず、行数と列数を指定してTableクラスをインスタンス化し、背景色、網掛け、罫線、行ごとの交互の色、幅を設定します。次に、[row, column]形式のインデックスで各セルにアクセスし、テキスト、画像、図形、段落、テーブルなどを追加します。最後に、テーブルをWord文書に追加します。
IronWordのテーブルを使うと、Word文書内の構造化データを柔軟に整理できます。請求書、レポート、データの要約などを作成する際に、Tableクラスで内容と表示形式を細かく設定できます。0から始まるインデックスで各セルを順に処理し、豊富なスタイル設定で体裁を整えられます。
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
WordDocument doc = new WordDocument();
// Create table
Table table = new Table(5, 3);
// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders()
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle,
};
// Apply styling
table.Zebra = new ZebraColor("FFFFFF", "dddddd");
table.Style.Borders = tableBorders;
// Populate table
table[0, 0] = new TableCell(new TextContent("Number"));
table[0, 1] = new TableCell(new TextContent("First Name"));
table[0, 2] = new TableCell(new TextContent("Last Name"));
for (int i = 1; i < table.Rows.Count; i++)
{
table[i, 0].AddChild(new TextContent($"{i}"));
table[i, 1].AddChild(new TextContent($"---"));
table[i, 2].AddChild(new TextContent($"---"));
}
// Add table
doc.AddTable(table);
doc.Save("document.docx");using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
WordDocument doc = new WordDocument();
// Create table
Table table = new Table(5, 3);
// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders()
{
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle,
};
// Apply styling
table.Zebra = new ZebraColor("FFFFFF", "dddddd");
table.Style.Borders = tableBorders;
// Populate table
table[0, 0] = new TableCell(new TextContent("Number"));
table[0, 1] = new TableCell(new TextContent("First Name"));
table[0, 2] = new TableCell(new TextContent("Last Name"));
for (int i = 1; i < table.Rows.Count; i++)
{
table[i, 0].AddChild(new TextContent($"{i}"));
table[i, 1].AddChild(new TextContent($"---"));
table[i, 2].AddChild(new TextContent($"---"));
}
// Add table
doc.AddTable(table);
doc.Save("document.docx");Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = BorderValues.Thick
borderStyle.BorderSize = 5
' Configure table border
Dim tableBorders As New TableBorders() With {
.TopBorder = borderStyle,
.RightBorder = borderStyle,
.BottomBorder = borderStyle,
.LeftBorder = borderStyle
}
' Apply styling
table.Zebra = New ZebraColor("FFFFFF", "dddddd")
table.Style.Borders = tableBorders
' Populate table
table(0, 0) = New TableCell(New TextContent("Number"))
table(0, 1) = New TableCell(New TextContent("First Name"))
table(0, 2) = New TableCell(New TextContent("Last Name"))
For i As Integer = 1 To table.Rows.Count - 1
table(i, 0).AddChild(New TextContent($"{i}"))
table(i, 1).AddChild(New TextContent("---"))
table(i, 2).AddChild(New TextContent("---"))
Next
' Add table
doc.AddTable(table)
doc.Save("document.docx")Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums
Dim doc As New WordDocument()
' Create table
Dim table As New Table(5, 3)
' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = BorderValues.Thick
borderStyle.BorderSize = 5
' Configure table border
Dim tableBorders As New TableBorders() With {
.TopBorder = borderStyle,
.RightBorder = borderStyle,
.BottomBorder = borderStyle,
.LeftBorder = borderStyle
}
' Apply styling
table.Zebra = New ZebraColor("FFFFFF", "dddddd")
table.Style.Borders = tableBorders
' Populate table
table(0, 0) = New TableCell(New TextContent("Number"))
table(0, 1) = New TableCell(New TextContent("First Name"))
table(0, 2) = New TableCell(New TextContent("Last Name"))
For i As Integer = 1 To table.Rows.Count - 1
table(i, 0).AddChild(New TextContent($"{i}"))
table(i, 1).AddChild(New TextContent("---"))
table(i, 2).AddChild(New TextContent("---"))
Next
' Add table
doc.AddTable(table)
doc.Save("document.docx")
TableCellクラスのAddChildメソッドには、IWordDocumentObjectを実装する段落、画像、図形、テーブルなどを渡せます。これにより、複雑な用途に対応する入れ子のテーブルを作成できます。
IronWordでは、テーブルのセルに内容を設定する方法が複数あります。コンストラクターに初期内容を渡してTableCellを作成する方法と、AddChildメソッドで内容を順次追加する方法があります。たとえば、1つのセルに見出しの段落、画像、詳細仕様を示す入れ子のテーブルを順に配置できます。
次の例では、複数の種類の内容をセルに追加します。
// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();
// Add a styled paragraph
Paragraph header = new Paragraph();
TextContent headerText = new TextContent("Product Details") { IsBold = true, FontSize = 14 };
header.AddText(headerText);
complexCell.AddChild(header);
// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001") { IsBold = true });
complexCell.AddChild(new TextContent("\nPrice: $49.99"));
// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord// Example: Creating cells with mixed content
TableCell complexCell = new TableCell();
// Add a styled paragraph
Paragraph header = new Paragraph();
TextContent headerText = new TextContent("Product Details") { IsBold = true, FontSize = 14 };
header.AddText(headerText);
complexCell.AddChild(header);
// Add multiple text elements
complexCell.AddChild(new TextContent("SKU: "));
complexCell.AddChild(new TextContent("PROD-001") { IsBold = true });
complexCell.AddChild(new TextContent("\nPrice: $49.99"));
// Cells can also contain lists, images, and more
// This demonstrates the versatility of table cells in IronWord' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.IsBold = True, .FontSize = 14}
header.AddText(headerText)
complexCell.AddChild(header)
' Add multiple text elements
complexCell.AddChild(New TextContent("SKU: "))
complexCell.AddChild(New TextContent("PROD-001") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $49.99"))
' Cells can also contain lists, images, and more
' This demonstrates the versatility of table cells in IronWord' Example: Creating cells with mixed content
Dim complexCell As New TableCell()
' Add a styled paragraph
Dim header As New Paragraph()
Dim headerText As New TextContent("Product Details") With {.IsBold = True, .FontSize = 14}
header.AddText(headerText)
complexCell.AddChild(header)
' Add multiple text elements
complexCell.AddChild(New TextContent("SKU: "))
complexCell.AddChild(New TextContent("PROD-001") With {.IsBold = True})
complexCell.AddChild(New TextContent(vbCrLf & "Price: $49.99"))
' Cells can also contain lists, images, and more
' This demonstrates the versatility of table cells in IronWordテーブルにどのようなスタイルを適用できますか?
IronWordには、読みやすく体裁の整ったテーブルを作成するためのスタイル設定が豊富にあります。罫線や色に加え、セルの余白や配置を調整し、行ごとに交互に色を付ける条件付き書式も適用できます。プロパティ名と列挙値から設定内容が分かりやすく、細かな調整が可能です。
利用できる罫線のスタイル
指定できる罫線のスタイルは、BorderValues列挙型で確認できます。

BorderValues列挙型では、シンプルな一重線から波線や点線などの複雑なパターンまで、用途に応じた罫線を選べます。ビジネス文書にはDoubleやTriple、デザインを重視する文書にはWaveやDashDotが適しています。BorderSizeプロパティをBorderValueと組み合わせると、線の太さを1/8ポイント単位で調整できます。
次の例では、罫線に異なる設定を適用します。
// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);
// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
BorderColor = Color.Navy,
BorderValue = BorderValues.Double,
BorderSize = 8
};
BorderStyle bodyBorder = new BorderStyle
{
BorderColor = Color.Gray,
BorderValue = BorderValues.Dotted,
BorderSize = 3
};
// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Style.Borders = new TableBorders
{
TopBorder = headerBorder,
BottomBorder = headerBorder,
LeftBorder = bodyBorder,
RightBorder = bodyBorder,
InsideHorizontalBorder = bodyBorder,
InsideVerticalBorder = bodyBorder
};
// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");// Example: Applying different borders to table sections
Table styledTable = new Table(4, 4);
// Create distinct border styles for header and body
BorderStyle headerBorder = new BorderStyle
{
BorderColor = Color.Navy,
BorderValue = BorderValues.Double,
BorderSize = 8
};
BorderStyle bodyBorder = new BorderStyle
{
BorderColor = Color.Gray,
BorderValue = BorderValues.Dotted,
BorderSize = 3
};
// Apply different borders to different parts of the table
// This creates visual hierarchy and improves readability
styledTable.Style.Borders = new TableBorders
{
TopBorder = headerBorder,
BottomBorder = headerBorder,
LeftBorder = bodyBorder,
RightBorder = bodyBorder,
InsideHorizontalBorder = bodyBorder,
InsideVerticalBorder = bodyBorder
};
// Zebra striping for better row distinction
styledTable.Zebra = new ZebraColor("F5F5F5", "FFFFFF");' Example: Applying different borders to table sections
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.BorderColor = Color.Gray,
.BorderValue = BorderValues.Dotted,
.BorderSize = 3
}
' Apply different borders to different parts of the table
' This creates visual hierarchy and improves readability
styledTable.Style.Borders = New TableBorders With {
.TopBorder = headerBorder,
.BottomBorder = headerBorder,
.LeftBorder = bodyBorder,
.RightBorder = bodyBorder,
.InsideHorizontalBorder = bodyBorder,
.InsideVerticalBorder = bodyBorder
}
' Zebra striping for better row distinction
styledTable.Zebra = New ZebraColor("F5F5F5", "FFFFFF")' Example: Applying different borders to table sections
Dim styledTable As New Table(4, 4)
' Create distinct border styles for header and body
Dim headerBorder As New BorderStyle With {
.BorderColor = Color.Navy,
.BorderValue = BorderValues.Double,
.BorderSize = 8
}
Dim bodyBorder As New BorderStyle With {
.BorderColor = Color.Gray,
.BorderValue = BorderValues.Dotted,
.BorderSize = 3
}
' Apply different borders to different parts of the table
' This creates visual hierarchy and improves readability
styledTable.Style.Borders = New TableBorders With {
.TopBorder = headerBorder,
.BottomBorder = headerBorder,
.LeftBorder = bodyBorder,
.RightBorder = bodyBorder,
.InsideHorizontalBorder = bodyBorder,
.InsideVerticalBorder = bodyBorder
}
' Zebra striping for better row distinction
styledTable.Zebra = New ZebraColor("F5F5F5", "FFFFFF")テーブルの幅と配置のプロパティを使うと、レイアウトをさらに細かく調整できます。幅は固定値またはパーセンテージで指定でき、文書内での配置や周囲のコンテンツとの位置関係も調整できます。セルごとに背景色、テキストの配置、余白を設定できます。
これらの設定により、シンプルなデータ一覧から、情報の階層を視覚的に区別した複雑な財務諸表まで、文書のデザイン要件に合ったテーブルを作成できます。
よくある質問
C#を使用してDOCXファイルにテーブルを作成するにはどうすればいいですか?
IronWordを使用してDOCXファイルにテーブルを作成するには、Tableクラスを行と列の数を指定してインスタンス化します。スタイルとコンテンツを設定した後、テーブルをWordDocumentに追加し、DOCXファイルとして保存できます。
IronWordでテーブルのためのスタイリングオプションには何がありますか?
IronWordは、テーブル用に境界線、色、セルの余白、配置などの豊富なスタイリングオプションを提供します。ゼブラストライピングのような条件付き書式を適用し、BorderValues列挙型からさまざまな境界線スタイルを利用して、テーブルの美観を向上させることができます。
IronWordを使用して個々のテーブルセルに複雑なコンテンツを追加できますか?
はい、IronWordでは、AddChildメソッドを使用して個々のテーブルセルに複雑なコンテンツを追加できるようにしています。セルには、入れ子になったテーブル、段落、画像、およびさまざまなテキスト要素を含めることができ、幅広い詳細なコンテンツ管理が可能です。
IronWordで作成されたテーブルでゼブラストライピングを使用するにはどうすればいいですか?
テーブルでゼブラストライピングを適用するには、IronWordのテーブルスタイルのZebraColorプロパティを設定し、交互に行の色を指定して読みやすさと視覚的な魅力を向上させます。
IronWordを使用してWordドキュメントにテーブルを追加する手順はどのようなものですか?
IronWordを使用してWord文書にテーブルを追加するには、IronWordライブラリをダウンロードし、テーブルを作成してスタイルを設定し、それをWordDocumentに挿入し、文書を保存する必要があります。
IronWordでは、テーブルの幅とアライメントをどのように制御しますか?
IronWordでは、テーブルの幅とアライメントをテーブルの適切なプロパティを設定することで制御します。これにより、必要に応じて正確な寸法やパーセンテージを指定し、ドキュメント内でテーブルを整列することができます。
IronWordでテーブルにデータを挿入するための方法は何がありますか?
IronWordでは、ゼロベースのインデックスを介してセルにアクセスし、各セルにTextContentや画像、その他の要素をプログラムで挿入するためのAddChildメソッドなどを使用して、テーブルを埋めることができます。
IronWordではテーブルのボーダースタイルをどのようにサポートしていますか?
IronWordでは、BorderStyleクラスを使用して境界線の色、値、サイズをカスタマイズすることで、ボーダースタイリングをサポートしています。テーブル内で、ヘッダーと本文のセクションに独自のビジュアル階層を作成できます。
IronWordを使用してセル内にテーブルをネストできますか?
はい、IronWordは、AddChildメソッドを使用してセル内にテーブルを入れ子にすることをサポートしています。この機能は、複雑な文書のために複雑なテーブル構造を構築するのに役立ちます。
Wordドキュメントでテーブルを作成するためにIronWordを使用する利点は何ですか?
IronWordを使用してテーブルを作成することで、ドキュメントの構造に対する柔軟でプログラム的なアプローチを提供し、包括的なコンテンツ管理、詳細なスタイリング、およびプロフェッショナルなDOCXファイルを生成するための効率的な自動化が可能です。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。