IRONWORDの使用

C# Word編集(コード例開発者チュートリアル)

Kannaopat Udonpant
カンナパット・ウドンパント
2023年11月14日
共有:

Word文書の作成、編集、および管理は、多くのアプリケーションで頻繁に要求される作業です。 C#(シーシャープ)でWord文書を作成・編集する方法はいくつかあるが、最も強力な方法の1つは、Microsoft Interopサービスを使用することである。 このツールを使えば、Word文書をプログラムで簡単に扱うことができる。

前提条件

環境をセットアップしてコードを書き始める前に、以下の前提条件を満たしていることを確認してください:

  1. Visual Studio: マシンに Visual Studio がインストールされていることを確認してください。インストールされていない場合は、Microsoft の公式サイトからダウンロードしてインストールしてください。

  2. Microsoft Word: Microsoft Interopを使用しているため、コンピュータにMS Wordをインストールする必要があります。 Interopサービスは、お使いのマシンにインストールされているMicrosoft Wordアプリケーションとのインターフェイスを提供します。

  3. 基本的なC#の知識 基本的なC#の理解

  4. .NET Framework: 私たちのアプリケーションはこれを基にしているため、Visual Studio が .NET Framework をサポートしていることを確認してください。

環境の設定

まず、Visual Studioアプリケーションを開きます。 開くと、ウェルカムスクリーンが迎えてくれる。

1.新しい.NETフレームワーク・コンソール・アプリケーションの作成

  1. 「Create a new project」をクリックしてください。

  2. 検索ボックスに「Console App (.NET Framework)」と入力してください。

  3. 結果から「コンソール アプリ (.NET Framework)」を選択し、「次へ」ボタンをクリックします。

  4. プロジェクト名を設定し、「作成」ボタンをクリックします。

    これらの手順の後、Visual Studioはあなたのために新しい.NETフレームワークのコンソールアプリケーションを生成します。 Program.cs ファイルには、コンソールアプリケーションのエントリーポイントである Main メソッドを備えた基本的なテンプレートがあります。

2. NuGet パッケージ マネージャーを使用して Microsoft.Office.Interop.Word をインストール

NuGetは.NET用のパッケージ・マネージャーで、Visual Studioに統合されている。 次の方法でMicrosoft.Office.Interop.Wordパッケージをインストールできます:

  1. Visual Studioの「ツール」メニューを開きます。

  2. NuGet Package Manager "を選択し、"Manage NuGet Packages for Solution... "を選択します。

  3. NuGetウィンドウで、"Browse "タブをクリックする。

  4. 検索ボックスにMicrosoft.Office.Interop.Wordと入力し、Enterキーを押します。

  5. 検索結果から、Microsoft.Office.Interop.Word パッケージを選択します。

  6. 右側で、コンソール・アプリケーション・プロジェクトがチェックされていることを確認し、「インストール」ボタンをクリックします。

    C# 編集 Word(コード例 開発者チュートリアル)図1

    Visual Studioはパッケージをインストールし、プロジェクトへの参照を追加します。 このパッケージには、C#アプリケーションからMS Wordとやり取りするために必要なアセンブリとツールが含まれています。

IronWord のご紹介:インターロップに代わる優れた選択肢

InteropはWordやExcelを操作するための強力な機能を提供するが、限界もある。 .NET開発者向けに最適化された多機能ライブラリ、IronWord が登場した。 IronWord は、特にWord文書の編集作業において、Interopよりもスムーズな使い心地を提供します。 互換性とパフォーマンスを保証するだけでなく、直感的な方法で複雑なタスクを簡素化する。 比較しやすくするために、それぞれのユースケースに対するMS Wordの後にIronWordのコードスニペットを提供します。IronWordバージョン2024.1.2を使用します。

既存のWord文書を開く

しばしば、既存のWord文書を編集する必要があるかもしれません、次の例は、C#(シーシャープ)でこれを行う方法を示しています:

var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
Dim WordDoc = WordApp.Documents.Open("path_to_your_document.docx")
$vbLabelText   $csharpLabel

上記のコードで、path_to_your_document.docx をあなたの docx ファイル のパスに置き換えます。

IronWordの使用

IronWord を使ってWord文書を開きます。

WordDocument doc = new WordDocument(@"path_to_your_document.docx");
WordDocument doc = new WordDocument(@"path_to_your_document.docx");
Dim doc As New WordDocument("path_to_your_document.docx")
$vbLabelText   $csharpLabel

新しいWord文書を作成する

ワード文書をゼロから作成する

var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Add();
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Add();
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
Dim WordDoc = WordApp.Documents.Add()
$vbLabelText   $csharpLabel

このコード・スニペットは、C#(シーシャープ)を使って作成・編集できる新しいWord文書を作成します。

IronWordの使用

WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
$vbLabelText   $csharpLabel

C#(シーシャープ)を使用;

Word文書にテキストを追加する

新しい段落を追加するには

WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";
WordDoc.Paragraphs.Add()
WordDoc.Paragraphs (1).Range.Text = "This is the first paragraph."
$vbLabelText   $csharpLabel

Paragraphs.Add() メソッドは、Word ドキュメントに新しい段落を追加し、Range.Text プロパティはそれに新しいテキストを割り当てます。

IronWordの使用

doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
$vbLabelText   $csharpLabel

既存のテキストの編集

このチュートリアルでは、最初の段落を変更しましょう:

WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";
WordDoc.Paragraphs (1).Range.Text = "This is the edited first paragraph."
$vbLabelText   $csharpLabel

同様の方法で、Word文書内の他の要素を追加・編集することもできます。

IronWordの使用

doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";
doc.Paragraphs (0).TextRuns (0).Text = "This is the edited first paragraph."
$vbLabelText   $csharpLabel

文書の保存と終了

目的の編集が終わったら

WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
WordDoc.SaveAs("path_where_you_want_to_save.docx")
WordDoc.Close()
WordApp.Quit()
$vbLabelText   $csharpLabel

path_where_you_want_to_save.docxを希望のパスに置き換えてください。

IronWordの使用

doc.SaveAs(@"path_where_you_want_to_save.docx");
doc.SaveAs(@"path_where_you_want_to_save.docx");
doc.SaveAs("path_where_you_want_to_save.docx")
$vbLabelText   $csharpLabel

完全なコードと例

それをまとめてみよう。 ここでは、既存のWord文書を開き、編集し、変更を保存する方法を示す完全なコード例を示します:

var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
Dim WordApp = New Microsoft.Office.Interop.Word.Application()

' Create a new Word document
Dim WordDoc = WordApp.Documents.Add()

' Add new text
WordDoc.Paragraphs.Add()
WordDoc.Paragraphs (1).Range.Text = "This is the first paragraph."

' Edit the first paragraph
WordDoc.Paragraphs (1).Range.Text = "This is the edited first paragraph."

' Save and close
WordDoc.SaveAs("path_where_you_want_to_save.docx")
WordDoc.Close()
WordApp.Quit()
$vbLabelText   $csharpLabel

IronWordの使用

MS Wordと比較した完全なコード例。 IronWord はDOCXファイルの編集に簡潔なコードスニペットを使用します。

// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit text
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";

// Export docx
doc.SaveAs(@"path_where_you_want_to_save.docx");
// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit text
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";

// Export docx
doc.SaveAs(@"path_where_you_want_to_save.docx");
' Create an empty Word document
Dim doc As New WordDocument()

' Add new text
doc.AddText("This is the first paragraph.")

' Edit text
doc.Paragraphs (0).TextRuns (0).Text = "This is the edited first paragraph."

' Export docx
doc.SaveAs("path_where_you_want_to_save.docx")
$vbLabelText   $csharpLabel

結論

.NETアプリケーション内でWordやExcelの文書を操作する領域では、選択肢が豊富にある。 マイクロソフトのインターロップ・サービスは多くの人が利用してきたが、IronWordのようなソリューションの登場は、より効率的でユーザーフレンドリーなツールへのシフトを意味する。

Kannaopat Udonpant
カンナパット・ウドンパント
ソフトウェアエンジニア
ソフトウェアエンジニアになる前に、カンナパットは日本の北海道大学から環境資源学の博士号を取得しました。学位を取得する過程で、カンナパットはバイオプロダクション工学部に所属する車両ロボティクス研究所のメンバーにもなりました。2022年には、C#のスキルを活かしてIron Softwareのエンジニアリングチームに参加し、IronPDFに注力しています。カンナパットは、IronPDFで使用されているコードの大部分を作成した開発者から直接学べることに価値を見いだしています。同僚との学び合いに加えて、Iron Softwareで働くことの社会的側面も楽しんでいます。コードやドキュメントを書いていない時には、カンナパットは通常、PS5でゲームをしたり、『The Last of Us』を再視聴したりしています。
< 以前
C#でWordドキュメントを作成する方法
次へ >
C#でWordをPDFに変換する方法