C# 編輯 Word(代碼示例開發者教程)
建立、編輯和管理Word 文件是許多應用程式的常見需求。 雖然在 C# 中有多種方法可以建立和編輯 Word 文檔,但最強大的方法之一是使用 Microsoft Interop 服務。 借助此工具,您可以非常輕鬆地以程式設計方式處理 Word 文件。
編輯 Word 和 DOCX 文檔
- 下載 C# 庫以編輯 Word 和 DOCX 文件
- 在代碼中創建一個空的 Word 文件
- 向 Word 文件添加新文字
- 編輯新文檔或現有文檔中的文字
- 將 DOCX 文件導出到所需位置
先決條件
在建立環境和開始編寫程式碼之前,請確保滿足以下先決條件:
- Visual Studio:請確認您的電腦已安裝 Visual Studio。若尚未安裝,請從微軟官方網站下載並安裝。
- Microsoft WORD:由於我們使用 Microsoft Interop,您的電腦上應已安裝 Microsoft WORD。 Interop 服務與您電腦上安裝的 Microsoft Word 應用程式互動。
- 基礎 C# 知識:理解基礎 C# 概念至關重要。
- .NET Framework:請確認您的 Visual Studio 支援 .NET Framework,因為我們的應用程式將以此為基礎。
營造環境
首先開啟 Visual Studio 應用程式。 打開後,您將看到歡迎畫面。
1. 建立一個新的 .NET Framework 控制台應用程式
- 點選"建立新項目"。
- 在搜尋框中輸入"控制台應用程式(.NET Framework)"。
- 從結果中,選擇"控制台應用程式 (.NET Framework)",然後按一下"下一步"按鈕。
- 為您的專案命名,然後按一下"建立"按鈕。
完成這些步驟後,Visual Studio 將為您產生一個新的 .NET Framework 控制台應用程式。 在Program.cs檔案中,你會找到一個基本模板,其中包含一個 Main 方法,這是控制台應用程式的入口點。
2. 使用 NuGet 套件管理器安裝 Microsoft.Office.Interop.Word
NuGet 是 .NET 的套件管理器,它整合在 Visual Studio 中。 以下是如何使用它來安裝 Microsoft.Office.Interop.Word 軟體包:
- 在 Visual Studio 中,前往"工具"功能表。
- 選擇"NuGet 套件管理員",然後選擇"管理解決方案的 NuGet 套件..."。
- 在 NuGet 視窗中,按一下"瀏覽"標籤。
- 在搜尋框中輸入
Microsoft.Office.Interop.Word,然後按下回車鍵。 - 從搜尋結果中,選擇
Microsoft.Office.Interop.Word軟體包。 - 在右側,請確保選取您的控制台應用程式項目,然後按一下"安裝"按鈕。
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");
' Create an instance of the Word Application
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
' Open a Word document with the specified file path
Dim WordDoc = WordApp.Documents.Open("path_to_your_document.docx")
在上面的程式碼中,將 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");
' Open a Word document with the specified file path using IronWord
Dim doc As New WordDocument("path_to_your_document.docx")
建立新的 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();
' Initialize a new instance of the Word Application
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
' Add a new Word document
Dim WordDoc = WordApp.Documents.Add()
這段程式碼片段會建立一個新的 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();
' Create a new, empty Word document using IronWord
Dim doc As New WordDocument()
為 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.";
' 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."
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");
' Add a new text to the document using IronWord
doc.AddText("Add text using IronWord")
編輯現有文字
在此教學中,讓我們修改第一段:
// 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.";
' Edit the text of the first paragraph
WordDoc.Paragraphs(1).Range.Text = "This is the edited first paragraph."
您也可以使用類似的方法在 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.";
' Edit the text of the first paragraph using IronWord
doc.Paragraphs(0).TextRuns(0).Text = "This is the edited first paragraph."
儲存並關閉文檔
完成所需的修改後:
// 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();
' 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()
將 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");
' Save the document to the desired path using IronWord
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()
使用 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");
' Create an empty Word document
Dim doc As 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")
結論
在 .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 方法的實際應用。


