跳過到頁腳內容
使用 IRONWORD

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

建立、編輯和管理Word 文件是許多應用程式的常見需求。 雖然在 C# 中有多種方法可以建立和編輯 Word 文檔,但最強大的方法之一是使用 Microsoft Interop 服務。 借助此工具,您可以非常輕鬆地以程式設計方式處理 Word 文件。

先決條件

在建立環境和開始編寫程式碼之前,請確保滿足以下先決條件:

  1. Visual Studio:請確保您的電腦上已安裝Visual Studio 。如果沒有,請從微軟官方網站下載並安裝。
  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. 在搜尋框中輸入"控制台應用程式(.NET Framework)"。
  3. 從結果中,選擇"控制台應用程式(.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,然後按下回車鍵。
  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# 中執行此操作:

// Create an instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Open a Word document with the specified file path
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
// Create an instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Open a Word document with the specified file path
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
$vbLabelText   $csharpLabel

在上面的程式碼中,將 path_to_your_document.docx 替換為你的docx 檔案的路徑。

使用IronWord

使用IronWord開啟 Word 文件。

// Open a Word document with the specified file path using IronWord
WordDocument doc = new WordDocument(@"path_to_your_document.docx");
// Open a Word document with the specified file path using IronWord
WordDocument doc = new WordDocument(@"path_to_your_document.docx");
$vbLabelText   $csharpLabel

建立新的 Word 文檔

從頭開始建立 Word 文件:

// Initialize a new instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Add a new Word document
var WordDoc = WordApp.Documents.Add();
// Initialize a new instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Add a new Word document
var WordDoc = WordApp.Documents.Add();
$vbLabelText   $csharpLabel

這段程式碼片段會建立一個新的 Word 文檔,您可以使用 C# 進行編寫和編輯。

使用IronWord

// Create a new, empty Word document using IronWord
WordDocument doc = new WordDocument();
// Create a new, empty Word document using IronWord
WordDocument doc = new WordDocument();
$vbLabelText   $csharpLabel

為 Word 文件新增文本

若要新增新的文字段落:

// Add a new paragraph to the document
WordDoc.Paragraphs.Add();
// Assign text to the newly added paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";
// Add a new paragraph to the document
WordDoc.Paragraphs.Add();
// Assign text to the newly added paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";
$vbLabelText   $csharpLabel

Paragraphs.Add() 方法在 Word 文件中新增一個新段落,而 Range.Text 屬性則為其指派新文字。

使用IronWord

// Add a new text to the document using IronWord
doc.AddText("Add text using IronWord");
// Add a new text to the document using IronWord
doc.AddText("Add text using IronWord");
$vbLabelText   $csharpLabel

編輯現有文本

在本教程中,我們來修改第一段:

// Edit the text of the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";
// Edit the text of the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";
$vbLabelText   $csharpLabel

您也可以使用類似的方法在 Word 文件中新增和編輯其他元素。

使用IronWord

// Edit the text of the first paragraph using IronWord
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";
// Edit the text of the first paragraph using IronWord
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";
$vbLabelText   $csharpLabel

儲存並關閉文檔

完成所需的修改後:

// Save the document to a specified path
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
// Close the document and quit the application
WordDoc.Close();
WordApp.Quit();
// Save the document to a specified path
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
// Close the document and quit the application
WordDoc.Close();
WordApp.Quit();
$vbLabelText   $csharpLabel

path_where_you_want_to_save.docx 替換為您所需的路徑。

使用IronWord

// Save the document to the desired path using IronWord
doc.SaveAs(@"path_where_you_want_to_save.docx");
// Save the document to the desired path using IronWord
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();
$vbLabelText   $csharpLabel

使用IronWord

使用IronWord 的完整IronWord範例非常簡潔。 IronWord 利用簡潔的程式碼片段來編輯 DOCX 檔案。

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

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

// Edit the 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 the 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 文件方面,選擇非常多。 雖然微軟的 Interop 服務一直是許多人的首選,但IronWord等解決方案的出現標誌著人們正在轉向更有效率、更人性化的工具。

常見問題解答

如何在 C# 中創建和編輯 Word 文檔?

您可以使用 Microsoft Interop 服務或 IronWord 庫在 C# 中創建和編輯 Word 文檔。這兩種選擇都允許您以編程方式操作 Word 文檔,而 IronWord 提供了更高的性能和更簡單的使用方法。

使用 IronWord 而非 Microsoft Interop 操作 Word 文檔有何好處?

IronWord 比 Microsoft Interop 提供了更流暢的體驗,因為它提供了更好的性能和更直觀的方法來編輯 Word 文檔。它針對 .NET 應用程序進行了優化,使其成為開發人員的現代高效選擇。

如何使用 C# 打開現有的 Word 文檔?

要使用 C# 打開現有的 Word 文檔,您可以使用 Microsoft Interop 服務或 IronWord 等庫。它們提供方法以編程方式加載和操控 Word 文件的內容。

設置 .NET Framework 控制台應用程序以編輯 Word 文檔需要哪些步驟?

首先,確保您安裝了 Visual Studio 和 .NET Framework。 在 Visual Studio 中創建新的 .NET Framework 控制台應用程序,並使用 NuGet 包管理器安裝 Microsoft.Office.Interop.Word 包或 IronWord 庫。

如何使用 C# 向 Word 文檔添加文本?

您可以使用 Microsoft Interop 或 IronWord 之類的庫來向 Word 文檔添加文本。這些庫提供方法來以編程方式插入和修改文檔中的文本。

如何在 C# 中保存和關閉 Word 文檔?

在 C# 中,您可以使用 Microsoft Interop 或 IronWord 等庫提供的方法來保存和關閉 Word 文檔。這些方法確保更改已保存且文檔正確關閉。

在 Visual Studio 中安裝 Microsoft.Office.Interop.Word 包的過程是什麼?

要在 Visual Studio 中安裝 Microsoft.Office.Interop.Word 包,通過“工具”菜單進入 NuGet 包管理器,選擇“為方案管理 NuGet 包...”,搜索“Microsoft.Office.Interop.Word”,然後安裝該包。

如何故障排除使用 C# 編輯 Word 文檔時的常見錯誤?

使用 C# 編輯 Word 文檔時的常見錯誤通常可以通過檢查正確的庫安裝、確保與 .NET Framework 的兼容性以及驗證文檔路徑和權限是否正確來解決。

如何在 C# 中創建一個新的 Word 文檔?

您可以使用 Microsoft Interop 或 IronWord 等庫在 C# 中創建新的 Word 文檔。這些庫提供方法來初始化新的 Word 文檔並根據需要添加內容。

IronWord 是否有完整的編輯 Word 文檔的代碼範例?

是的,教程提供了使用 IronWord 編輯 Word 文檔的完整代碼範例。它包括創建 Word 應用程序實例、添加和編輯文本以及保存文檔,演示了 IronWord 方法的實際應用。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me