透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
Word文書の作成、編集、および管理は、多くのアプリケーションで頻繁に要求される作業です。 C#(シーシャープ)でWord文書を作成・編集する方法はいくつかあるが、最も強力な方法の1つは、Microsoft Interopサービスを使用することである。 このツールを使えば、Word文書をプログラムで簡単に扱うことができる。
環境をセットアップしてコードを書き始める前に、以下の前提条件を満たしていることを確認してください:
Visual Studio: マシンに Visual Studio がインストールされていることを確認してください。インストールされていない場合は、Microsoft の公式サイトからダウンロードしてインストールしてください。
Microsoft Word: Microsoft Interopを使用しているため、コンピュータにMS Wordをインストールする必要があります。 Interopサービスは、お使いのマシンにインストールされているMicrosoft Wordアプリケーションとのインターフェイスを提供します。
基本的なC#の知識 基本的なC#の理解
まず、Visual Studioアプリケーションを開きます。 開くと、ウェルカムスクリーンが迎えてくれる。
「Create a new project」をクリックしてください。
検索ボックスに「Console App (.NET Framework)」と入力してください。
結果から「コンソール アプリ (.NET Framework)」を選択し、「次へ」ボタンをクリックします。
プロジェクト名を設定し、「作成」ボタンをクリックします。
これらの手順の後、Visual Studioはあなたのために新しい.NETフレームワークのコンソールアプリケーションを生成します。 Program.cs ファイルには、コンソールアプリケーションのエントリーポイントである Main
メソッドを備えた基本的なテンプレートがあります。
Microsoft.Office.Interop.Word
をインストールNuGetは.NET用のパッケージ・マネージャーで、Visual Studioに統合されている。 次の方法でMicrosoft.Office.Interop.Word
パッケージをインストールできます:
Visual Studioの「ツール」メニューを開きます。
NuGet Package Manager "を選択し、"Manage NuGet Packages for Solution... "を選択します。
NuGetウィンドウで、"Browse "タブをクリックする。
検索ボックスにMicrosoft.Office.Interop.Word
と入力し、Enterキーを押します。
検索結果から、Microsoft.Office.Interop.Word
パッケージを選択します。
右側で、コンソール・アプリケーション・プロジェクトがチェックされていることを確認し、「インストール」ボタンをクリックします。
Visual Studioはパッケージをインストールし、プロジェクトへの参照を追加します。 このパッケージには、C#アプリケーションからMS Wordとやり取りするために必要なアセンブリとツールが含まれています。
InteropはWordやExcelを操作するための強力な機能を提供するが、限界もある。 .NET開発者向けに最適化された多機能ライブラリ、IronWord が登場した。 IronWord は、特にWord文書の編集作業において、Interopよりもスムーズな使い心地を提供します。 互換性とパフォーマンスを保証するだけでなく、直感的な方法で複雑なタスクを簡素化する。 比較しやすくするために、それぞれのユースケースに対するMS Wordの後にIronWordのコードスニペットを提供します。IronWordバージョン2024.1.2を使用します。
しばしば、既存の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")
上記のコードで、path_to_your_document.docx をあなたの docx ファイル のパスに置き換えます。
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")
ワード文書をゼロから作成する
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()
このコード・スニペットは、C#(シーシャープ)を使って作成・編集できる新しいWord文書を作成します。
WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
新しい段落を追加するには
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."
Paragraphs.Add()
メソッドは、Word ドキュメントに新しい段落を追加し、Range.Text
プロパティはそれに新しいテキストを割り当てます。
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
このチュートリアルでは、最初の段落を変更しましょう:
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."
同様の方法で、Word文書内の他の要素を追加・編集することもできます。
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."
目的の編集が終わったら
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()
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");
doc.SaveAs("path_where_you_want_to_save.docx")
それをまとめてみよう。 ここでは、既存の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()
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")
.NETアプリケーション内でWordやExcelの文書を操作する領域では、選択肢が豊富にある。 マイクロソフトのインターロップ・サービスは多くの人が利用してきたが、IronWordのようなソリューションの登場は、より効率的でユーザーフレンドリーなツールへのシフトを意味する。