IRONWORDの使用

Office Interopを使用せずにC#でWordドキュメントを作成する方法

更新済み 3月 12, 2024
共有:

Microsoft Word文書は、正式なビジネスレポートから個人的な手紙まで、さまざまな用途に広く使用されています。 C#(シーシャープ)では、開発者はしばしばMicrosoft Word文書をプログラムで生成する必要がある。 Windowsアプリケーション開発者は従来、Microsoft Office Interopを使ってC#(シーシャープ)を使ってWord文書を生成・作成してきた。

しかし、このアプローチは誰もが利用できるものではなく、開発者はマイクロソフト・オフィスのインターフェイスが利用できないOSやLinuxマシンを使っていることもある。 このような場合、開発者は異なるプラットフォームで独立して動作する他のライブラリを探さなければならない。 プログラム的にWordファイルを操作するための強力なライブラリの1つは、次のとおりです。 IronWord (アイアンワード)より [Iron Software (アイアンソフトウェア)**](/).

IronWord (アイアンワード)は、.NETアプリケーションでWord文書を扱うための堅牢な機能を提供し、さまざまなプラットフォームやLinuxベースのDockerイメージ/コンテナで実行できます。 IronWord (シーシャープ、VB.NET用ワード、DocxドキュメントAPI)を使えば、Microsoft Office、Officeオートメーション、Word Interopをインストールすることなく、ワード文書ファイルのビルド、編集、エクスポートが可能です。 IronWordは.NET 8、7、6、フレームワーク、コア、Azureを完全にサポートしています。

この記事では、IronWord(アイアンワード)ライブラリを使ってC#(シーシャープ)でWord文書を作成する方法について説明します。

C#(シーアンドナム)でOfficeを介さずにWord文書を作成する方法;

  1. 新しいC#プロジェクトを作成してください。

  2. インストール アイアンワード(IronWord) ライブラリ。

  3. IronWord (アイアンワード) ライブラリを使用してワード文書を作成します。

  4. 既存の文書にコンテンツを追加する。

  5. 作成したWord文書を保存します。

  6. 作成したWord文書を開いて表示する。

    **前提条件:***。

  7. Visual Studio: Visual Studio または他の C# 開発環境がインストールされていることを確認してください。

  8. NuGetパッケージマネージャー: プロジェクト内のパッケージ管理にNuGetを使用できるようにしてください。

ステップ1:新しいC#プロジェクトを作成する

C#(シーシャープ)コンソールアプリケーションを新規作成するか、Word文書を生成したい既存のプロジェクトを使用します。

コンソールアプリケーションテンプレートを選択し、「次へ」をクリックします。

C#(シーシャープ)でOfficeを介さずにWord文書を作成する方法:図1 - 新しいプロジェクトを作成するためのコンソールアプリのテンプレート

次のステップでは、ソリューション名とプロジェクト名を入力します。

C#(シーシャープ)でOfficeを介さずにWord文書を作成する方法:図2 - ソリューションとプロジェクト名でプロジェクトを構成する

.NET Versionを選択し、「作成」をクリックします。

C#でOfficeを介さずにWord文書を作成する方法 (シーシャープ):図3 正しい.NETバージョンでプロジェクトを作成する

ステップ 2: IronWordライブラリのインストール (アイアンワード・ライブラリー)

C# (シーシャープ)プロジェクトを開き、インストールします。 IronWord ライブラリを使用します:

Install-Package IronWord -Version 2024.1.2

NuGetパッケージは、以下のようにVisual Studioパッケージ・マネージャーを使用してインストールすることもできます。

C#(シーシャープ)でOfficeを介さずにWord文書を作成する方法:図4 - VSパッケージマネージャによるアイアンワードのインストール

ステップ3: IronWord (アイアンワードライブラリ)を使ったワード文書の作成と保存

IronWord (アイアンワード) ライブラリを使ってワード文書を作成する簡単な例から始めましょう。 次のコードは、"Hello, World "というテキストを含む1つの段落からなるWord文書を作成する方法を示しています。!"

using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
using IronWord;
using IronWord.Models;
// Create textrun instance
TextRun textRun = new TextRun("Hello, World!");
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a new Word document object
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("example.docx"); // save docx file to disk
Imports IronWord
Imports IronWord.Models
' Create textrun instance
Private textRun As New TextRun("Hello, World!")
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a new Word document object
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("example.docx") ' save docx file to disk
VB   C#

上記のコード例を実行すると、新しい文書ファイルexample.docxが作成され、出力は以下のようになります。

C#(シーシャープ)でOfficeを介さずにWord文書を作成する方法:図5 - 上記のコードで作成したWord文書。

IronWord (アイアンワード)を使ってWord文書ファイルを生成する基本的な例です。 詳細については、以下の文書を参照してください。 [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](/csharp/word/docs/).

Word文書にスタイル付き段落を追加する。

IronWord (アイアンワード)を使って簡単なWord文書を作成する方法がわかったので、段落とスタイル付きテキストを追加してみましょう。

また、TextRunはスタイルデータを取り込み、テキストの視覚的表現を強化することもできる。 テキストには取り消し線、太字、斜体、下線などのスタイルを設定できる。

以下のコードを修正し、以前作成したプログラムに追加してください。

using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
    IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle); 
// add italic string
TextStyle boldStyle = new TextStyle()
{
    IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle); 
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create textrun
TextRun textRun = new TextRun("Hello, World!"); // add string
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Configure text
TextRun introText = new TextRun("This is an example paragraph with italic and bold styling."); // add string or text
TextStyle italicStyle = new TextStyle()
{
    IsItalic = true
};
TextRun italicText = new TextRun("Italic example sentence.", italicStyle); 
// add italic string
TextStyle boldStyle = new TextStyle()
{
    IsBold = true
};
TextRun boldText = new TextRun("Bold example sentence.", boldStyle); 
// add bold string
// Add text
paragraph.AddTextRun(introText);
paragraph.AddTextRun(italicText);
paragraph.AddTextRun(boldText);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx document
doc.SaveAs("example.docx");// docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create textrun
Private textRun As New TextRun("Hello, World!") ' add string
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Configure text
Dim introText As New TextRun("This is an example paragraph with italic and bold styling.") ' add string or text
Dim italicStyle As New TextStyle() With {.IsItalic = True}
Dim italicText As New TextRun("Italic example sentence.", italicStyle)
' add italic string
Dim boldStyle As New TextStyle() With {.IsBold = True}
Dim boldText As New TextRun("Bold example sentence.", boldStyle)
' add bold string
' Add text
paragraph.AddTextRun(introText)
paragraph.AddTextRun(italicText)
paragraph.AddTextRun(boldText)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx document
doc.SaveAs("example.docx") ' docx file save in EXE location
VB   C#

Word文書に表を追加する

データを明確に表現するために、グリッドで表現することもできる。 データを格子状に並べたものをテーブルと呼ぶ。 IronWord (アイアンワード)を使って、以下のようにWord文書に表や画像を追加することができます:

using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style 
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.Color.Black);
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders() { 
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};
cell.Borders = tableBorders;
// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
TextRun textRun = new TextRun();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddContent(new Paragraph(textRun));
// Configure border style 
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = new IronColor(IronSoftware.Drawing.Color.Black);
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders() { 
    TopBorder = borderStyle,
    RightBorder = borderStyle,
    BottomBorder = borderStyle,
    LeftBorder = borderStyle,
};
cell.Borders = tableBorders;
// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell); // add cell
row.AddCell(cell); // add cell
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Generate document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx"); // docx file save in EXE location
Imports IronWord
Imports IronWord.Models
' Create table cell
Private cell As New TableCell()
Private textRun As New TextRun()
textRun.Text = "Sample text"
' Add textrun to the cell
cell.AddContent(New Paragraph(textRun))
' Configure border style 
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = New IronColor(IronSoftware.Drawing.Color.Black)
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5
' Configure table border
Dim tableBorders As New TableBorders() With {
	.TopBorder = borderStyle,
	.RightBorder = borderStyle,
	.BottomBorder = borderStyle,
	.LeftBorder = borderStyle
}
cell.Borders = tableBorders
' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell) ' add cell
row.AddCell(cell) ' add cell
' Create table and add row
Dim table As New Table()
table.AddRow(row)
' Generate document from the table
Dim doc As New WordDocument(table)
' Export Word document
doc.SaveAs("Document.docx") ' docx file save in EXE location
VB   C#

上の例では、ボーダーを使ってWord文書に表を追加した。

Word文書に画像を追加する

画像は文書の体裁を整え、色彩や視覚的な魅力を増すことができる。

IronWord (アイアンワード)を使えば、以下のコードのようにWord文書にプログラムで画像を追加することができます:

using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph 
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
using IronWord;
using IronWord.Models;
// Load new document
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("SalesChart.jpg");
image.Width = 200; // In unit pixel (px)
image.Height = 200; // In unit pixel (px)
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph 
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx"); // docx file
Imports IronWord
Imports IronWord.Models
' Load new document
Private doc As New WordDocument()
' Configure image
Private image As New IronWord.Models.Image("SalesChart.jpg")
image.Width = 200 ' In unit pixel (px)
image.Height = 200 ' In unit pixel (px)
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph 
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx") ' docx file
VB   C#

ここでは、IronWord.Models.Imageを使用して、高さと幅が200ピクセルの画像を段落に追加しています。

ライセンス(無料トライアル利用可能)

アイアンワードの使用にはライセンスが必要です。 試用キーはアイアンソフトウェア(Iron Software)のウェブサイトから入手できます。 これ. このキーはappsettings.jsonに配置する必要があります。

{
    "IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
{
    "IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
}
If True Then
	"IronWord.LicenseKey":"IRONWORD.MYLICENSE.KEY.TRIAL"
End If
VB   C#

試用ライセンスを取得するためにメールアドレスを提供してください。 電子メールIDを送信すると、キーが電子メールで配信されます。

C#(シーシャープ)でOfficeを介さずにWord文書を作成する方法:図6 - 試用フォームが正常に送信されたときのポップアップ

結論

C#(シーシャープ)を使ってWord文書を作成する IronWord (アイアンワード) ライブラリはMicrosoft Officeに依存せずに文書を作成する柔軟で効率的な方法を提供します。シンプルなレターから表や画像を使った複雑なレポートまで、IronWord (アイアンワード) を使えばプログラムで作成することができます。 この記事では、Word文書を作成するための包括的なチュートリアルを提供します。 IronWord (アイアンワード)を使えば、Wordドキュメントの作成を自動化し、C# (シーシャープ)アプリケーションの汎用性と生産性を高めることができます。

また、生成されたWord文書と連動してPDFファイルを操作したい開発者には、アイアンソフトウェア(Iron Software)のIronPDF(C#ライブラリ)をお勧めします。 をクリックしてください。 [以下の内容を日本語に翻訳します:

ここに

ご希望のイディオムや技術用語が追加されることによって、より適切な翻訳が提供できる場合もありますので、詳細なコンテキストを教えていただけると幸いです。](https://ironpdf.com/).

< 以前
VB .NETでプログラムでWordドキュメントを作成する方法
次へ >
C#でフォーマットを保持してWordドキュメントを読み取る方法

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 4,489 View Licenses >