跳至頁尾內容
USING IRONWORD

如何在 C# 中將 Word 轉換為 PDF

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

先決條件

在設置環境並開始程式碼之前,確保您滿足以下先決條件:

  1. Visual Studio:確保在您的機器上已安裝Visual Studio。如果沒有,請從Microsoft官方網站下載並安裝它。
  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並按下回車。
  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

在上述程式碼中,用您的docx文件的路徑替換path_to_your_document.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

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文件的領域中,選擇豐富。 雖然Microsoft的Interop服務一直是許多人的首選,但IronWord這樣的解決方案的出現表明向更高效和使用者友好的工具轉移。

常見問題

如何在C#中建立和編輯Word文件?

您可以使用Microsoft Interop服務或IronWord程式庫在C#中建立和編輯Word文件。這兩種選擇都允許您以程式化方式操作Word文件,而IronWord提供了增強的性能和使用方便性。

使用IronWord操作Word文件相比Microsoft Interop有什麼優勢?

IronWord比Microsoft Interop提供了更順暢的體驗,通過提供更好的性能和更直觀的方法來編輯Word文件。它優化於.NET應用程式,是開發者的現代高效選擇。

如何使用C#打開現有的Word文件?

要使用C#打開現有的Word文件,您可以使用像Microsoft Interop服務或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方法的實用應用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話