跳過到頁腳內容
使用 IRONWORD

C# 編輯 Word(代碼示例開發者教程)

建立、編輯和管理Word 文件是許多應用程式的常見需求。 雖然在 C# 中有多種方法可以建立和編輯 Word 文檔,但最強大的方法之一是使用 Microsoft Interop 服務。 借助此工具,您可以非常輕鬆地以程式設計方式處理 Word 文件。

先決條件

在建立環境和開始編寫程式碼之前,請確保滿足以下先決條件:

  1. Visual Studio :請確保您的電腦上已安裝Visual Studio 。如果沒有,請從微軟官方網站下載並安裝。
  2. Microsoft Word :由於我們使用 Microsoft Interop,因此您的電腦上應該安裝MS Word 。 Interop 服務與您電腦上安裝的 Microsoft Word 應用程式互動。
  3. C# 基礎:理解 C# 的基本概念至關重要。
  4. .NET Framework :確保您的 Visual Studio 支援 .NET Framework,因為我們的應用程式將基於它。

營造環境

首先開啟 Visual Studio 應用程式。 打開後,您將看到歡迎畫面。

1. 建立一個新的 .NET Framework 控制台應用程式

  1. 點選"建立新項目"。
  2. 在搜尋框中輸入"控制台應用程式(.NET Framework)"。
  3. 從結果中,選擇"控制台應用程式 (.NET Framework)",然後按一下"下一步"按鈕。
  4. 為您的專案命名,然後按一下"建立"按鈕。

完成這些步驟後,Visual Studio 將為您產生一個新的 .NET Framework 控制台應用程式。 在Program.cs檔案中,你會找到一個有Main方法的基本模板,它是控制台應用程式的入口點。

2. 使用 NuGet 套件管理器安裝Microsoft.Office.Interop.Word

NuGet 是 .NET 的套件管理器,它整合在 Visual Studio 中。 以下是如何使用它來安裝Microsoft.Office.Interop.Word套件:

  1. 在 Visual Studio 中,前往"工具"功能表。
  2. 選擇"NuGet 套件管理員",然後選擇"管理解決方案的 NuGet 套件..."。
  3. 在 NuGet 視窗中,按一下"瀏覽"標籤。
  4. 在搜尋框中,鍵入Microsoft.Office.Interop.Word ,然後按 Enter 鍵。
  5. 從搜尋結果中,選擇Microsoft.Office.Interop.Word套件。
  6. 在右側,請確保選取您的控制台應用程式項目,然後按一下"安裝"按鈕。

! 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 方法的實際應用。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。