如何使用 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 都能滿足您的所有需求。
它最顯著的特徵如下:
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"
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對象,並將字串"範例文字"賦值給它。
-
然後我們實例化一個Paragraph對象,並將"textRunExample"加入到該物件中。
-
我們也實例化了一個WordDocument對象,將其命名為
WordDocument doc,,並將paragraphExample傳遞給它,以建立一個包含該段落的新 Word 文件。 -
程式碼將 Word 文件儲存為檔案名稱"document.docx",以便稍後使用。
-
要存取我們剛剛建立的 Word 文件中的段落,我們可以存取WordDocument物件的"Paragraphs"屬性。 "Paragraphs"屬性是一個列表。因此,我們需要使用foreach循環來遍歷它。
-
要取得段落中的文字,我們可以存取Paragraphs的"Texts"屬性。 這也會傳回一個文字清單。
- 最後,我們將文字賦值給名為"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 的豐富功能一段時間,因為它提供免費試用授權。 除了易於使用之外,IronWord 還附帶詳盡的文件和 24/5 全天候支援,從而減輕開發人員在生產過程中可能遇到的持續挫折感。 我們還提供各種教學課程和一系列程式碼範例供您參考,以協助您開始使用IronWord 。
在使用試用許可證測試IronWord的各個方面之後,您可以購買我們的 Lite 開發人員許可證,起價為 599 美元,價格從 Lite 到 Professional 依次遞增。 更多資訊請參閱我們的許可頁面。
常見問題解答
我怎麼能使用 C# 讀取 Word 文檔?
您可以使用 IronWord 在 C# 中讀取 Word 文檔。只需使用 WordDocument 類加載文檔,然後遍歷段落和文本運行以訪問和顯示文本內容。
什麼是可靠的 C# 庫用于讀取 Word 文檔?
IronWord 是一個可靠的 C# 庫,專為讀取和操作 Word 文檔而設計。它提供了一個簡單的 API,可以簡化應用程序中文檔功能的集成。
我需要安裝 Microsoft Office 來用 C# 讀取 Word 文檔嗎?
不,IronWord 不需要安裝 Microsoft Office。它獨立運行,使您可以在不需要 Office 授權版的情況下操作 Word 文檔。
我如何在 Visual Studio 中安裝一個 C# 庫來讀取 Word 文檔?
您可以通過在 Visual Studio 的 NuGet 包管理器中搜索 'IronWord' 並選擇 'Install',或使用 NuGet 包管理器控制台並使用命令 Install-Package IronWord 來安裝 IronWord。
IronWord 支持哪些平台用于文檔操作?
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 將 Word 文檔轉換為多種格式,通過利用其 API 將文檔導出為例如 PDF, HTML 和其他格式,儘管具體的轉換可能會有所不同。
在 C# 應用程序中 IronWord 的一些常見用例是什麼?
IronWord 常用於填寫模板、生成報告、批量處理文檔和將 Word 文檔功能集成到 .NET 應用程序中的任務。
IronWord 用戶有哪些支援選項?
IronWord 提供 24/5 支援,並提供全面的文檔、教程和代碼範例,以幫助開發者有效地利用該庫。


