使用 IRONWORD C# 編輯 Word (程式碼範例開發人員教程) Curtis Chau 更新:2025年7月28日 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 建立、編輯和管理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套件。 在右側,請確保選取您的控制台應用程式項目,然後按一下"安裝"按鈕。 ! 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"); ' 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") $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"); ' Open a Word document with the specified file path using IronWord Dim doc As 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(); ' 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() $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(); ' Create a new, empty Word document using IronWord Dim doc As 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."; ' 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"); ' 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."; ' 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."; ' 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(); ' 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"); ' 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(); 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 使用 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") $vbLabelText $csharpLabel 結論 在 .NET 應用程式中操作 Word 和 Excel 文件方面,選擇非常多。 雖然微軟的 Interop 服務一直是許多人的首選,但 IronWord 等解決方案的出現標誌著人們正在轉向更有效率、更人性化的工具。 常見問題解答 如何用 C# 建立和編輯 Word 文件? 您可以使用 Microsoft Interop 服務或 IronWord 函式庫以 C# 來建立和編輯 Word 文件。這兩種選項都可讓您以程式化的方式操作 Word 文件,其中 IronWord 可提供更佳的效能與易用性。 與 Microsoft Interop 相比,使用 IronWord 進行 Word 文件操作有哪些優點? 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 library。 如何使用 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 套件管理員,選擇「Manage NuGet Packages for Solution...」,搜尋「Microsoft.Office.Interop.Word」,然後安裝該套件。 使用 C# 編輯 Word 文件時,如何排除常見錯誤? 使用 C# 編輯 Word 文件時的常見錯誤通常可以透過檢查適當的函式庫安裝、確保與 .NET Framework 相容,以及驗證文件路徑和權限是否正確來解決。 如何在 C# 中建立新的 Word 文件? 您可以使用 Microsoft Interop 或 IronWord 之類的函式庫,在 C# 中建立新的 Word 文件。這些函式庫提供初始化新 Word 文件的方法,並可視需要新增內容。 是否有使用 IronWord 編輯 Word 文件的完整程式碼範例? 是的,本教程提供了使用 IronWord 編輯 Word 文檔的完整程式碼範例。它包括創建 Word 應用程式實例、新增和編輯文字,以及儲存文件,展示了 IronWord 方法的實際應用。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2026年1月22日 如何使用 C# 和 IronWord 在 Word 文件中建立可填寫的表格範本 學習如何使用 IronWord 在 C# 中建立可填寫的表單範本。 閱讀更多 更新2025年9月18日 ASP .NET Core 匯入和匯出 Word 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新2025年10月11日 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多 如何在 C# 中建立 Word 文件如何在 C# 中將 Word 轉換為 PDF
更新2025年10月11日 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多