使用IRONWORD

C# 編輯 Word (代碼範例開發者教程)

發佈 2023年11月14日
分享:

創建、編輯和管理Word 文件是許多應用程式中的常見需求。雖然在 C# 中有多種方法可以創建和編輯 Word 文件,但其中最強大的方法之一是使用 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 Framework 主控台應用程式

  1. 點擊「創建新的專案」。
  2. 輸入「Console App」 (.NET框架)"搜尋框內。

  3. 從結果中選擇 "Console App (.NET框架)然後點擊“下一步”按鈕。

  4. 為您的專案設定一個名稱,然後點擊“建立”按鈕。

完成這些步驟後,Visual Studio 將為您生成一個新的 .NET Framework 主控台應用程式。在 Program.cs 檔案中,您會找到一個包含 Main 方法的基本模板,這是主控台應用程式的入口點。

2. 使用 NuGet 套件管理器安裝 Microsoft.Office.Interop.Word

NuGet 是 .NET 的套件管理器,並且集成到 Visual Studio 中。以下是使用 NuGet 安裝 Microsoft.Office.Interop.Word 套件的步驟:

  1. 在 Visual Studio 中,進入 "工具" 菜單。

  2. 選擇 "NuGet 套件管理員",然後選擇 "為解決方案管理 NuGet 套件...”。

  3. 在 NuGet 視窗中,點擊 "瀏覽" 標籤。

  4. 在搜索框中輸入 Microsoft.Office.Interop.Word,並按 Enter 鍵。

  5. 在搜索結果中,選擇 Microsoft.Office.Interop.Word 套件。

  6. 在右側,確保選中你的控制台應用程式專案,然後點擊 "安裝" 按鈕。

C# 编辑 Word(代码示例开发者教程)图1

Visual Studio 現在將會安裝這個套件並在您的專案中新增一個引用。這個套件包含了與MS Word互動所需的程序集和工具,讓您的 C# 應用程式可以使用。

介紹 IronWord:Interop 的出色替代品

雖然 Interop 提供了強大的功能來處理 Word 和 Excel,但它也有其局限性。這時候就有了 IronWord,一個為 .NET 開發者優化的多功能庫。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")
VB   C#

在以上代碼中,將 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")
VB   C#

新建 Word 文件

從頭開始創建 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#

Paragraphs.Add()method 將新段落添加到 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_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")
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 文件的領域中,有很多選擇。雖然微軟的 Interop 服務一直是許多人的首選,但像 IronWord 這樣的解決方案的出現標誌著向更高效和使用者友好的工具的轉變。

< 上一頁
如何在 C# 中建立 Word 文件
下一個 >
如何在C#中將Word轉換為PDF

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 5,614 查看許可證 >