如何使用 C# 操作 Word 文件
在當今時代,Microsoft Word文件已成為辦公工作以及專業和個人交流的代名詞。 因此,對於希望在應用程式中向使用者顯示自動化任務的開發人員來說,以程式化方式操縱Word文件至關重要。 儘管有許多可用的程式庫,但並非所有的都一樣可靠。 然而,有一個脫穎而出的競爭者是IronWord。 IronWord是一個可靠而強大的C# Word DOCX程式庫,易於使用和理解,並簡化了處理Word文件的過程。
這篇文章將探討如何快速利用IronWord來閱讀Word文件,通過簡短範例展示。
How To Read a Word File Using C
-
在Visual Studio中建立一個控制台應用程式
-
安裝IronWord C# DOCX程式庫
-
使用WordDocument類建立一個新的Word文件
-
向Word文件中新增文字
-
使用Paragraph類遍歷每個段落
- 顯示內容
IronWord:C# DOCX程式庫

IronWord是一個靈活且可擴展的程式庫,具有直接且易於使用的API,消除了將Word文件整合到應用程式中的麻煩。 無論您想在應用程式中新增和整合一個簡單的文字Word文件還是建立複雜的表格和報告以供使用者顯示,IronWord都能滿足需要。
以下是其最顯著的功能:
-
文件操作:IronWord允許開發人員輕鬆地操作Word文件。 無論使用者想要插入文字段落還是更複雜的結構,如表格和圖片,IronWord都可以做到。
-
跨平台支援和相容性:IronWord設計靈活,支援多平台開發人員。 它支援各種.NET Core(8、7、6、5和3.1+)、.NET Standard(2.0+)、.NET Framework(4.6.2+),甚至還有Azure。 此外,開發人員可以在不同的平台和系統上使用IronWord,包括但不限於Windows、Linux、macOS和Android。 它涵蓋了最常見的平台,允許開發人員快速構建跨平台應用程式。
-
不依賴於Microsoft Office:在.NET應用程式中整合Word文件時的一個常見問題是常用程式庫如Interop需要安裝有許可的Microsoft Office。不過,IronWord透過獨立實現不受此限制的影響。 開發人員可以充分利用Word文件的強大功能,而不受Microsoft Word許可和安裝的限制。
-
格式選項:IronWord提供了廣泛的格式和樣式支援,使開發人員可以使文件獨特。 開發人員可以應用文字的字體、顏色,對齊方式和其他複雜的格式,如表格樣式。
- 易用性和廣泛支援:除了易於理解的API和直接的方法調用外,IronWord還提供擴展API參考和程式碼範例,幫助開發人員找出最優化的利用IronWord的方法。
在Visual Studio中建立新的控制台項目
在我們深入範例之前,先在Visual Studio中建立一個空的控制台項目。

然後我們提供項目的名稱和保存位置。

接下來,選擇您將使用的.NET框架。 在此範例中,我將使用.NET 8.0。

在建立並設置好新的控制台項目後,我們來安裝我們的C# word程式庫,IronWord。
安裝IronWord
有兩種方式來安裝IronWord。
1. 通過NuGet套件管理器安裝
通過NuGet套件管理器安裝,點擊工具然後Manage NuGet Packages for Solution。 然後我們在搜索欄中搜索IronWord並安裝**IronWord**。

2. 通過NuGet套件管理器控制台安裝
另一種方法是通過NuGet套件管理器控制台安裝。 為此,請在控制台中運行以下命令:
Install-Package IronWord
許可金鑰
請注意,IronWord不能在沒有許可金鑰的情況下運行; 您可以作為免費試用的一部分在此處獲取。
獲得試用金鑰後,確保在您的項目中設置此變數。
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
Reading Word Documents in C
安裝完先決條件後,我們將在下面的程式碼中展示使用IronWord閱讀Microsoft Word文件的簡單性。
首先,我們導入必要的命名空間。 使用IronWord,我們建立一個新文件並新增樣本文字。 然後我們使用WordDocument物件存取段落和文字,以便在Word文件中列印出文字。
using IronWord;
using IronWord.Models;
#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion
// Create text run with sample text
Text textRunExample = new Text("Sample text");
// Create a paragraph and add the text run to it
Paragraph paragraphExample = new Paragraph();
paragraphExample.AddChild(textRunExample);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraphExample);
// Export the document as a DOCX file
doc.SaveAs("document.docx");
// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (Text textRun in paragraph.Texts)
{
// Access text content
string content = textRun.Text;
// Display the content to the console
Console.WriteLine(content);
}
}
using IronWord;
using IronWord.Models;
#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion
// Create text run with sample text
Text textRunExample = new Text("Sample text");
// Create a paragraph and add the text run to it
Paragraph paragraphExample = new Paragraph();
paragraphExample.AddChild(textRunExample);
// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraphExample);
// Export the document as a DOCX file
doc.SaveAs("document.docx");
// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (Text textRun in paragraph.Texts)
{
// Access text content
string content = textRun.Text;
// Display the content to the console
Console.WriteLine(content);
}
}
Imports IronWord
Imports IronWord.Models
#Region "Licensing"
' Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE"
'#End Region
' Create text run with sample text
Dim textRunExample As New Text("Sample text")
' Create a paragraph and add the text run to it
Dim paragraphExample As New Paragraph()
paragraphExample.AddChild(textRunExample)
' Create a new Word document with the paragraph
Dim doc As New WordDocument(paragraphExample)
' Export the document as a DOCX file
doc.SaveAs("document.docx")
' Access paragraphs and text runs within the document
For Each paragraph As Paragraph In doc.Paragraphs
For Each textRun As Text In paragraph.Texts
' Access text content
Dim content As String = textRun.Text
' Display the content to the console
Console.WriteLine(content)
Next textRun
Next paragraph
讓我們來探討從上述程式碼中讀取Word文件的方法和參數。
-
我們首先建立一個Text物件,並將字串"Sample text"賦值給它。
-
然後我們實例化一個Paragraph物件,並將"textRunExample"新增到其中。
-
我們還實例化了一個WordDocument物件,將其命名為
paragraphExample傳遞給它,以建立包含該段落的新Word文件。 -
程式碼保存了名稱為"document.docx"的Word文件,以便以後使用。
-
要存取我們剛剛建立的Word文件中的段落,我們存取WordDocument物件的"Paragraphs"屬性。 "Paragraphs"屬性是一個列表。因此,我們調用一個foreach迴圈來迭代它。
-
要獲得段落中的文字,我們存取Paragraphs的"Texts"屬性。 這也返回了一個Text的列表。
- 最後,我們將Text分配給名為"content"的字串變數,並將其列印到控制台。
控制台輸出

Reading an existing Word document in C
在前面的範例中,我們程式化地建立了一個新的Word文件並讀取其內容。 我們可以通過稍作修改來讀取現有Word文件,遵循類似的步驟。
輸入文件

using IronWord;
using IronWord.Models;
#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion
// Load an existing Word document
WordDocument doc = new WordDocument("existing_document.docx");
// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (Text textRun in paragraph.Texts)
{
// Access text content
string content = textRun.Text;
// Display the content to the console
Console.WriteLine(content);
}
}
using IronWord;
using IronWord.Models;
#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion
// Load an existing Word document
WordDocument doc = new WordDocument("existing_document.docx");
// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (Text textRun in paragraph.Texts)
{
// Access text content
string content = textRun.Text;
// Display the content to the console
Console.WriteLine(content);
}
}
Imports IronWord
Imports IronWord.Models
#Region "Licensing"
' Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE"
'#End Region
' Load an existing Word document
Dim doc As New WordDocument("existing_document.docx")
' Access paragraphs and text runs within the document
For Each paragraph As Paragraph In doc.Paragraphs
For Each textRun As Text In paragraph.Texts
' Access text content
Dim content As String = textRun.Text
' Display the content to the console
Console.WriteLine(content)
Next textRun
Next paragraph
兩個範例之間的主要區別是在WordDocument物件中傳遞的參數。 我們將現有的Word文件載入到其中,而不是建立新文件。 其餘部分與其他範例相同。
控制台輸出

結論

在整個範例中,我們展示了如何使用IronWord程式庫在C#中以程式化方式操作和閱讀Word文件。 IronWord程式庫的靈活性和可擴展性使其成為一個有價值的工具,允許開發人員在實際生活範例中使用IronWord,如填寫模板、生成報告和批量處理文件。 了解如何將Word整合到應用程式中對於開發人員來說很有價值,因為這為他們的問題提供了更多解決方案。
此外,開發人員可以在購買之前測試IronWord's的廣泛功能,因為它提供免費試用授權。 除了易於使用,IronWord還提供詳細的文件和24/5開發人員支援,緩解開發過程中可能面臨的持續挫折。 我們還提供各種教程和一系列程式碼範例作為參考,幫助您開始使用IronWord。
在使用試用授權測試了IronWord的各個方面後,您可以購買我們的Lite開發者許可,起價為599美元,從Lite到Professional不等。 請參考我們的許可頁面以獲取更多資訊。
常見問題
如何使用 C# 讀取 Word 文件?
您可以使用 IronWord 在 C# 中讀取 Word 文件。只需使用 WordDocument 類別載入文件,然後遍歷段落和文字片段來存取和顯示文字內容。
有什麼可靠的 C# 程式庫可用來讀取 Word 文件?
IronWord 是專為讀取和操作 Word 文件而設計的可靠 C# 程式庫。它提供一個簡單明瞭的 API,簡化了 Word 文件功能的整合。
我需要安裝 Microsoft Office 來在 C# 中讀取 Word 文件嗎?
不需要,IronWord 無需安裝 Microsoft Office。它可以獨立作業,允許您操作 Word 文件而不需要 Office 的授權版本。
如何在 Visual Studio 中安裝用於讀取 Word 文件的 C# 程式庫?
您可以在 Visual Studio 的 NuGet 套件管理器中搜尋 'IronWord' 並選擇 'Install' 來安裝,或者使用 NuGet 套件管理器控制台輸入命令 Install-Package IronWord。
IronWord 支援哪些平台的 Word 文件操作?
IronWord 支援多種平台,包括 .NET Core(8、7、6、5 和 3.1+)、.NET Standard(2.0+)、.NET Framework(4.6.2+)和 Azure,並與 Windows、Linux、macOS 和 Android 相容。
我可以使用 C# 程式庫來操作現有的 Word 文件嗎?
是的,IronWord 允許您以程式化方式讀取和修改新建及現有的 Word 文件,提供對文件內容和結構的完全控制。
IronWord 有免費試用版嗎?
有的,IronWord 提供免費試用授權。開發者可以使用這個試用版來探索其功能並評估是否適合他們的專案,然後再購買完整授權。
如何使用 C# 將 Word 文件轉換為其他格式?
您可以使用 IronWord 利用其 API 將 Word 文件導出為 PDF、HTML 等多種格式,但具體的轉換細節可能有所不同。
IronWord 在 C# 應用程式中的一些常見使用情境是什麼?
IronWord 常用於如填寫模板、生成報告、批量處理文件以及將 Word 文件功能整合到 .NET 應用程式中。
IronWord 有哪些支援選項可供使用者選擇?
IronWord 提供24/5支援,並提供詳盡的文件、教程和範例程式碼,以協助開發者有效使用這個程式庫。



