使用IRONWORD

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

Kannaopat Udonpant
坎納帕特·烏頓潘
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 Framework)」。

  3. 從結果中選擇「Console App (.NET Framework)」,然後點擊「下一步」按鈕。

  4. 為您的專案設置名稱,然後點擊「建立」按鈕。

    完成這些步驟後,Visual Studio 將為您生成一個新的 .NET Framework 控制台應用程序。 在 Program.cs 文件中,您將找到一個包含 Main 方法的基本範本,該方法是控制台應用程式的進入點。

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

NuGet 是 .NET 的一個套件管理器,並且它已經整合到 Visual Studio 中。 以下是安裝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 現在會安裝該套件並在您的專案中新增對它的參考。 此套件包含必要的程序集和工具,以便從您的 C# 應用程式中與MS Word 進行互動。

介紹IronWord:Interop的卓越替代方案

雖然 Interop 為處理 Word 和 Excel 提供了強大的功能,但它也有其局限性。 進入IronWord,這是一個為 .NET 開發人員優化的多功能程式庫。 IronWord 提供比 Interop 更順暢的體驗,尤其是在編輯 Word 文件任務方面。 它不僅確保了相容性和效能,還透過直觀的方法簡化了複雜的任務。 為了方便比較,我將在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 文件

從頭開始建立 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 文件的領域中,有許多選擇。 雖然許多人一直依賴 Microsoft 的互操作服務,但像 IronWord 這樣的解決方案的出現意味著向更高效和用戶友好的工具的轉變。

Kannaopat Udonpant
坎納帕特·烏頓潘
軟體工程師
在成為軟體工程師之前,Kannapat 在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat 也成為了車輛機器人實驗室的成員,該實驗室隸屬於生物生產工程學系。2022 年,他利用自己的 C# 技能,加入了 Iron Software 的工程團隊,專注於 IronPDF 的開發。Kannapat 珍視這份工作,因為他可以直接向負責撰寫大部分 IronPDF 程式碼的開發人員學習。除了同儕學習外,Kannapat 還享受在 Iron Software 工作的社交方面。當他不在撰寫程式碼或文件時,Kannapat 通常會在 PS5 上玩遊戲或重看《最後生還者》。
< 上一頁
如何在 C# 中建立 Word 文件
下一個 >
如何在C#中將Word轉換為PDF

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

查看許可證 >