IRONWORDの使用

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

更新済み 11月 14, 2023
共有:

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

前提条件

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

  1. **ビジュアルスタジオ次のことを確認してください。 ビジュアルスタジオ をインストールしてください。インストールされていない場合は、マイクロソフトの公式サイトからダウンロードしてインストールしてください。

  2. マイクロソフト・ワード:Microsoft Interopを使用するため、以下のものが必要です。 エムエスワード お使いのコンピューターにインストールされています。 Interopサービスは、お使いのマシンにインストールされているMicrosoft Wordアプリケーションとのインターフェイスを提供します。

  3. **C# (シーシャープ)の基本的な知識;

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

環境の設定

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

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

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

  2. コンソールアプリ」と入力 (.NETフレームワーク)「を検索ボックスに入力する。

  3. 結果から「コンソールアプリ」を選択 (.NETフレームワーク)"をクリックし、"Next "ボタンをクリックする。

  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`」と入力してエンターキーを押します。

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

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

    C# (シーシャープ) ワードの編集 (コード例 開発者向けチュートリアル) 図1

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

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

InteropはWordやExcelを操作するための強力な機能を提供するが、限界もある。 .NET開発者向けに最適化された多機能ライブラリ、IronWord(アイアンワード)が登場した。 IronWord (アイアンワード)は、特にWord文書の編集作業において、Interopよりもスムーズな使い心地を提供します。 互換性とパフォーマンスを保証するだけでなく、直感的な方法で複雑なタスクを簡素化する。 比較を容易にするために、MSワードの後に、それぞれの使用例について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")
VB   C#

上記のコードで、path_to_your_document.docxdocxファイルへのパスに置き換える。

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")
VB   C#

新しい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()
VB   C#

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

IronWordの使用

WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
VB   C#

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."
VB   C#

パラグラフの追加()メソッドはWord文書に新しい段落を追加し、Range.Textプロパティはそれに新しいテキストを割り当てます。

IronWordの使用

doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
VB   C#

既存のテキストの編集

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

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."
VB   C#

同様の方法で、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."
VB   C#

文書の保存と終了

目的の編集が終わったら

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()
VB   C#

pathを希望のパスに置き換える。

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")
VB   C#

完全なコードと例

それをまとめてみよう。 ここでは、既存の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()
VB   C#

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")
VB   C#

結論

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

< 以前
C#でWordドキュメントを作成する方法
次へ >
C#でWordをPDFに変換する方法

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

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