如何使用 C# 閱讀 Word 文件
在當今時代,Microsoft Word 文件已成為辦公室工作以及專業和個人溝通的代名詞。 因此,對於希望在應用程式中自動執行向使用者顯示的任務的開發人員來說,以程式設計方式操作 Word 文件至關重要。 雖然有很多可用的庫,但並非所有庫都像其他庫一樣強大。 然而, IronWord就是眾多競爭者中脫穎而出的一個。 IronWord是一個可靠且強大的 C# Word DOCX 程式庫,易於使用和理解,簡化了 Word 文件的處理。
本文將透過簡短的範例,探討如何快速利用IronWord閱讀 Word 文件。
如何使用 C# 讀取 Word 文件
在 Visual Studio 中建立一個控制台應用程式
安裝 IronWord C# DOCX 函式庫
使用WordDocument類別建立一個新的 Word 文檔
在 Word 文件中新增文本
使用Paragraph類別遍歷每個段落。
- 顯示內容
IronWord:C# DOCX 庫
IronWord是一個靈活且可擴展的程式庫,它具有簡單易用的 API,可消除將 Word 文件整合到應用程式中的麻煩。 無論您是想在應用程式中新增和整合簡單的文字 Word 文檔,還是建立複雜的表格和報告以顯示給用戶,IronWord 都能滿足您的所有需求。
它最顯著的特徵如下:
1.文件操作:IronWord 允許開發人員輕鬆操作 Word 文件。 無論使用者想要插入文字段落還是更複雜的結構,例如表格和圖像,IronWord 都能做到。
2.跨平台支援與相容性: IronWord 的設計具有彈性,支援多個平台上的開發人員。 它支援各種 .NET Core(8、7、6、5 和 3.1+)、.NET Standard(2.0+)、.NET Framework(4.6.2+),甚至 Azure。 此外,開發者可以在不同的平台和系統上使用 IronWord,包括但不限於 Windows、Linux、macOS 和 Android。 它涵蓋了最常見的平台,並允許開發人員快速建立跨平台應用程式。
3.獨立於 Microsoft Office:在 .NET 應用程式中整合 Word 文件時,一個常見的問題是,像 Interop 這樣的常用程式庫需要安裝 Microsoft Office 的授權才能運作。然而,IronWord 透過獨立於此限制解決了這個問題。 開發者可以充分利用 Word 文件的強大功能,而不受 Microsoft Word 的授權和安裝限制。
4.格式選項:IronWord 提供廣泛的格式和樣式支持,使開發人員能夠使文件獨具特色。 開發人員可以為文字套用字體、顏色、對齊方式和其他複雜格式,例如表格樣式。
5.易用性和廣泛的支援:除了易於理解的 API 和直接的方法呼叫外,IronWord 還提供擴展 API 參考和程式碼範例,以幫助開發人員確定使用 IronWord 的最佳方式。
在 Visual Studio 中建立新的控制台項目
在深入範例之前,讓我們先在Visual Studio中建立一個空白的控制台專案。
然後我們為項目提供名稱和保存位置。
接下來,選擇您將要使用的 .NET 框架。 在這個範例中,我將使用.NET 8.0。
建立並設定新的控制台專案後,讓我們安裝我們的 C# 單字庫IronWord 。
安裝 IronWord
安裝 IronWord 有兩種方法。
1. 透過 NuGet 套件管理器安裝
若要透過 NuGet 套件管理員安裝它,請按一下"工具",然後Manage NuGet Packages for Solution 。 然後我們在搜尋列中搜尋 IronWord 並安裝**IronWord** 。
! 使用 NuGet 套件管理器管理解決方案中的 NuGet 套件來安裝 IronWord,方法是在 NuGet 套件管理器的搜尋列中搜尋"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"在 C# 中讀取 Word 文檔
安裝必備元件後,我們將透過以下程式碼示範使用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對象,並將字串"範例文字"賦值給它。
然後我們實例化一個Paragraph對象,並將"textRunExample"加入到該物件中。
我們也實例化了一個WordDocument對象,將其命名為
WordDocument doc,並將paragraphExample傳遞給它,以建立一個包含該段落的新 Word 文件。程式碼將 Word 文件儲存為檔案名稱"document.docx",以便稍後使用。
要存取我們剛剛建立的 Word 文件中的段落,我們可以存取WordDocument物件的"Paragraphs"屬性。 "Paragraphs"屬性是一個列表。因此,我們需要使用foreach循環來遍歷它。
要取得段落中的文本,我們可以存取Paragraphs的"Texts"屬性。 這也會傳回一個文字清單。
- 最後,我們將文字賦值給名為"content"的字串變量,並將其印到控制台。
控制台輸出
在 C# 中讀取現有的 Word 文檔
在前面的範例中,我們透過程式設計方式建立了一個新的 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 的豐富功能一段時間,因為它提供免費試用授權。 除了易於使用之外,IronWord 還附帶詳盡的文件和 24/5 全天候支持,從而減輕開發人員在生產過程中可能遇到的持續挫折感。 我們還提供各種教學課程和一系列程式碼範例供您參考,以協助您開始使用IronWord 。
在使用試用許可證測試IronWord的各個方面之後,您可以購買我們的 Lite 開發人員許可證,起價為 599 美元,價格從 Lite 到 Professional 依次遞增。 更多資訊請參閱我們的許可頁面。
常見問題解答
如何使用 C# 閱讀 Word 文件?
您可以使用 IronWord 以 C# 語言閱讀 Word 文件。只需使用 WordDocument 類載入文件,然後遍历段落和文字運行以存取和顯示文字內容。
讀取 Word 文件的可靠 C# 函式庫?
IronWord 是一個可靠的 C# 函式庫,專為閱讀和操作 Word 文件而設計。它提供了直接的 API,簡化了在應用程式中整合 Word 文件功能的過程。
我需要安裝 Microsoft Office 來閱讀 C# 語言的 Word 文件嗎?
不,IronWord 不需要安裝 Microsoft Office。它可以獨立運作,讓您無需 Office 授權版本即可處理 Word 文件。
如何在 Visual Studio 中安裝閱讀 Word 文件的 C# 函式庫?
您可以透過 Visual Studio 中的 NuGet 套件管理員來安裝 IronWord,方法是搜尋「IronWord」並選擇「安裝」,或使用 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 支援,以及全面的文件、教學和程式碼範例,協助開發人員有效運用程式庫。







