C# 編輯 Word(程式碼範例開發者教學)
建立、編輯和管理Word 文件是許多應用程式的常見需求。 雖然在 C# 中有多種方法可以建立和編輯 Word 文檔,但最強大的方法之一是使用 Microsoft Interop 服務。 借助此工具,您可以非常輕鬆地以程式設計方式處理 Word 文件。
編輯 Word 和 DOCX 文檔
- 下載用於編輯 Word 和 DOCX 文件的 C# 庫
- 用程式碼建立一個空的 Word 文檔
- 為 Word 文件新增文本
- 在新文件或現有文件中編輯文本
- 將 DOCX 檔案匯出到所需位置
先決條件
在建立環境和開始編寫程式碼之前,請確保滿足以下先決條件:
- Visual Studio:請確保您的電腦上已安裝Visual Studio 。如果沒有,請從微軟官方網站下載並安裝。
- Microsoft Word:由於我們使用 Microsoft Interop,因此您的電腦上應該安裝MS 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,然後按 Enter 鍵。 - 從搜尋結果中,選擇
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");在上面的程式碼中,將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");建立新的 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();這段程式碼片段會建立一個新的 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();為 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.";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");編輯現有文本
在本教程中,我們來修改第一段:
// 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.";儲存並關閉文檔
完成所需的修改後:
// 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");完整程式碼和範例
讓我們把所有內容整合起來。 以下是一個完整的程式碼範例,示範如何開啟現有的 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();使用 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");結論
在 .NET 應用程式中操作 Word 和 Excel 文件方面,選擇非常多。 雖然微軟的 Interop 服務一直是許多人的首選,但 IronWord 等解決方案的出現標誌著人們正在轉向更有效率、更人性化的工具。
常見問題解答
如何使用 C# 建立和編輯 Word 文件?
您可以使用 Microsoft Interop 服務或 IronWord 庫在 C# 中建立和編輯 Word 文件。這兩種方法都允許您以程式設計方式操作 Word 文檔,其中 IronWord 提供更高的效能和更易用性。
與使用 Microsoft Interop 進行 Word 文件操作相比,使用 IronWord 有哪些優點?
IronWord 相較於 Microsoft Interop,提供更流暢的使用體驗,具有更優異的效能和更直覺的 Word 文件編輯方式。它針對 .NET 應用程式進行了最佳化,是開發人員的現代化高效之選。
如何使用 C# 開啟現有的 Word 文件?
若要使用 C# 開啟現有的 Word 文檔,可以使用 Microsoft Interop Services 或 IronWord 等函式庫。它們提供了以程式設計方式載入和操作 Word 文件內容的方法。
要設定用於編輯 Word 文件的 .NET Framework 控制台應用程序,需要哪些步驟?
首先,請確保已安裝 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 方法的實際應用。







