IronWordでC#を使用してDOCXに表を追加する方法
IronWordを使用すると、開発者はTableオブジェクトを作成して、指定された行と列でC#でプログラム的にWord文書にテーブルを追加し、境界線や色でスタイリングし、内容を入力した後にDOCXファイルとして保存することができます。
この例では、IronWordでテーブルを作成する方法を示します。 寸法で表を作成し、スタイルを適用してコンテンツを追加し、文書に挿入して保存します。 スタイリングされた表を含むDOCXファイルを数分で作成できます。
-
1Install IronWord with NuGet Package Manager
-
2このコード スニペットをコピーして実行します。
var table = new IronWord.Models.Table(3,4); var doc = new IronWord.WordDocument(); doc.AddTable(table); doc.SaveAs("QuickTable.docx");C# -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronWord を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- DOCXにテーブルを追加するためのC#ライブラリをダウンロード
- セルにコンテンツを入力し、セルを行に組み立てる
- 行を追加してテーブルを作成する
- テーブルを伴う新しいWord文書を初期化し、テーブルを追加する
- 最終的なWord文書をエクスポートする
Word文書に表を追加するにはどうすればよいですか?
表はWord文書の基本的な構成要素です。 まず、行と列の数を指定してTableクラスをインスタンス化します。 背景色、シェーディング、ボーダー、ゼブラストライプ、幅など、テーブルのスタイルを設定します。 次に、直感的な[row, column]インデックスを使用して各セルにアクセスします。 各セルにテキスト、画像、図形、段落、あるいは表を追加します。 最後に、Word文書に表を追加します。
IronWordのテーブルは、Word文書で構造化されたデータを整理するための柔軟な基盤を提供します。 請求書、レポート、またはデータ要約を作成する場合、Tableクラスはコンテンツとプレゼンテーションに対する包括的なコントロールを提供します。 ゼロ基準のインデックスシステムはプログラム的なセルの反復を簡略化し、豊かなスタイリングオプションでプロフェッショナルな外観を保証します。
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");

ContentElementオブジェクトを受け入れます。 これにより、複雑なユースケースのネストテーブルが可能になります。
IronWordはテーブル・セルを扱う際に、コンテンツ管理のための複数のアプローチを提供します。 コンストラクターを使用して初期コンテンツで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
どのようなスタイリング オプションをテーブルに適用できますか?
IronWordはテーブルのための広範なスタイリング機能を提供し、視覚的に魅力的でプロフェッショナルな文書の作成を可能にします。 基本的なボーダーや色だけでなく、セルのパディングやアライメントを制御し、ゼブラストライプによる条件付き書式を適用します。 スタイリング・システムは、親しみやすいプロパティ名と明確な値の列挙を使用し、パワーと直感的なデザインを兼ね備えています。
どのボーダー スタイルが利用できますか?
BorderValues列挙型を使用して境界値のすべての利用可能なオプションを探索します:

BorderValues列挙型はテーブルの美観に対する包括的なオプションを提供します。 シンプルな単線から、波や点のような複雑なパターンまで、各スタイルは特定のデザイン目的に対応しています。 ビジネス文書にはプロフェッショナルなダブルボーダーやトリプルボーダーが、クリエイティブ文書にはWaveやDashDotパターンが効果的です。 BorderValueと連携して、8分の1ポイントで測定されるラインの厚さに対して正確なコントロールを提供します。
以下は、異なるボーダー構成を示す実用的な例です:
// 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.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");Imports System.Drawing
' 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.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")テーブルの幅と配置のプロパティは、追加のレイアウト制御を提供します。 テーブルを特定の幅またはパーセンテージに設定し、ドキュメント内で整列させ、周囲のコンテンツとの相互作用を制御します。 セルレベルのスタイリング・オプションには、個々の背景色、テキストの配置、パディングの調整が含まれ、テーブルの外観のあらゆる側面をきめ細かく制御できます。
これらのスタイリング・オプションにより、単純なデータ・グリッドから複数の視覚的階層を持つ複雑な財務諸表まで、あらゆる文書のデザイン要件に合った表を作成することができます。
よくある質問
Word文書で特定の寸法の表を作成するにはどうすればよいですか?
IronWordでは、Tableクラスをインスタンス化し、行数と列数を指定することでテーブルを作成することができます。例えば、'var table = new IronWord.Models.Table(3,4);'で3行×4列のテーブルを作成します。そしてそれをWordDocumentオブジェクトに追加し、DOCXファイルとして保存します。
プログラムでテーブルのボーダーや色を設定できますか?
IronWordでは、背景色、シェーディング、ボーダー、ゼブラストライプ、幅を含む包括的なテーブル・スタイルを設定することができます。Word文書に追加する前に、これらのスタイルをテーブル・オブジェクトに適用することができます。
テーブルの特定のセルにアクセスし、入力するにはどうすればよいですか?
IronWordはゼロベースのインデックスを使用してテーブルのセルにアクセスします。直感的な[行、列]表記でセルにアクセスし、テキスト、画像、図形、段落、ネストされたテーブルなど、さまざまな種類のコンテンツをセルに入力することができます。
テーブルセルにはどのようなコンテンツを追加できますか?
IronWordのTableCellクラスを使用すると、ContentElementオブジェクトを受け付けるAddChildメソッドを通して複数のコンテンツ・タイプを追加することができます。これには、段落、画像、図形、そして入れ子の表構造を作成するための表も含まれます。
テーブルのセル内に入れ子のテーブルを作成できますか?
はい、IronWordはネストされたテーブルをサポートしています。AddChildメソッドはテーブルを含むContentElementオブジェクトを受け付けるので、テーブル・セル内にテーブルを追加して、複雑なデータ構成を扱うことができます。
表付きのDOCXファイルを作成する最も簡単な方法は何ですか?
IronWordの最速のアプローチは、ディメンションを持つTableオブジェクトを作成し、WordDocumentをインスタンス化し、AddTable()でテーブルを追加し、SaveAs()で保存することです。このプロセス全体はわずか4行のコードで完了します。
What methods are available to populate a table with data in IronWord?
In IronWord, you can populate a table by accessing cells through zero-based indexing and using methods like `AddChild` to insert `TextContent`, images, or other elements into each cell programmatically.
How does IronWord support border styling for tables?
IronWord supports border styling by allowing you to customize border color, value, and size using the `BorderStyle` class. You can create distinct visual hierarchies within your tables for both header and body sections.
Can I nest tables within cells using IronWord?
Yes, IronWord supports nesting tables within cells by using the `AddChild` method. This feature assists in constructing intricate table structures for complex documents.
What are the benefits of using IronWord for table creation in Word documents?
Using IronWord for table creation provides a flexible and programmable approach to document structure, allowing comprehensive content management, detailed styling, and efficient automation for generating professional DOCX files.

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