在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
創建、編輯和管理Word 文檔是許多應用程序的經常性要求。 雖然在 C# 中有多種方法可以建立和編輯 Word 文件,但最強大的方法之一是使用 Microsoft Interop 服務。 使用這個工具,您可以輕鬆地以程式方式處理 Word 文件。
在設置環境並開始編寫代碼之前,請確保滿足以下先決條件:
Visual Studio:確保您的計算機上已安裝Visual Studio。如果沒有,請從 Microsoft 官方網站下載並安裝。
Microsoft Word:由於我們使用 Microsoft Interop,您應該在您的電腦上安裝MS Word。 Interop 服務介面與安裝在您機器上的 Microsoft Word 應用程式進行互動。
基本的C#知識 理解基本的C#
首先打開 Visual Studio 應用程式。 一旦開啟,您將看到歡迎畫面。
點擊「創建新專案」。
在搜尋框中輸入「Console App (.NET Framework)」。
從結果中選擇「Console App (.NET Framework)」,然後點擊「下一步」按鈕。
為您的專案設置名稱,然後點擊「建立」按鈕。
完成這些步驟後,Visual Studio 將為您生成一個新的 .NET Framework 控制台應用程序。 在 Program.cs 文件中,您將找到一個包含 Main
方法的基本範本,該方法是控制台應用程式的進入點。
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 進行互動。
雖然 Interop 為處理 Word 和 Excel 提供了強大的功能,但它也有其局限性。 進入IronWord,這是一個為 .NET 開發人員優化的多功能程式庫。 IronWord 提供比 Interop 更順暢的體驗,尤其是在編輯 Word 文件任務方面。 它不僅確保了相容性和效能,還透過直觀的方法簡化了複雜的任務。 為了方便比較,我將在MS Word之後,針對每個用例提供IronWord的代碼片段,使用IronWord版本2024.1.2。
通常,您可能需要編輯現有的 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")
在上面的程式碼中,將path_to_your_document.docx替換為docx檔案的路徑。
使用 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")
從頭開始建立 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()
這段程式碼片段使用 C# 新建一個可以撰寫和編輯的 Word 文件。
WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
要添加一個新的文字段落:
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."
Paragraphs.Add()
方法將新增段落添加到 Word 文件中,而 Range.Text
屬性則為其指派新文字。
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
對於本教程,讓我們變更第一段落:
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."
您也可以使用類似的方法添加和編輯 Word 文件中的其他元素。
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."
完成所需的編輯後:
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()
將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");
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()
完整的程式碼範例與 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")
在 .NET 應用程式中操作 Word 和 Excel 文件的領域中,有許多選擇。 雖然許多人一直依賴 Microsoft 的互操作服務,但像 IronWord 這樣的解決方案的出現意味著向更高效和用戶友好的工具的轉變。