在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
創建、編輯和管理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框架)"搜尋框內。
從結果中選擇 "Console App (.NET框架)然後點擊“下一步”按鈕。
完成這些步驟後,Visual Studio 將為您生成一個新的 .NET Framework 主控台應用程式。在 Program.cs 檔案中,您會找到一個包含 Main
方法的基本模板,這是主控台應用程式的入口點。
Microsoft.Office.Interop.Word
NuGet 是 .NET 的套件管理器,並且集成到 Visual Studio 中。以下是使用 NuGet 安裝 Microsoft.Office.Interop.Word
套件的步驟:
在 Visual Studio 中,進入 "工具" 菜單。
選擇 "NuGet 套件管理員",然後選擇 "為解決方案管理 NuGet 套件...”。
在 NuGet 視窗中,點擊 "瀏覽" 標籤。
在搜索框中輸入 Microsoft.Office.Interop.Word
,並按 Enter 鍵。
在搜索結果中,選擇 Microsoft.Office.Interop.Word
套件。
Visual Studio 現在將會安裝這個套件並在您的專案中新增一個引用。這個套件包含了與MS Word互動所需的程序集和工具,讓您的 C# 應用程式可以使用。
雖然 Interop 提供了強大的功能來處理 Word 和 Excel,但它也有其局限性。這時候就有了 IronWord,一個為 .NET 開發者優化的多功能庫。IronWord 在編輯 Word 文件任務方面提供了比 Interop 更流暢的體驗。它不僅保證了兼容性和性能,還通過直觀的方法簡化了複雜的任務。為便於比較,我將在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
()method
將新段落添加到 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 文件的領域中,有很多選擇。雖然微軟的 Interop 服務一直是許多人的首選,但像 IronWord 這樣的解決方案的出現標誌著向更高效和使用者友好的工具的轉變。