C# 列印 Word 教程:逐步指南
Word應用文件是專業和個人溝通中不可或缺的一部分。 能夠以程式方式操作和互動Microsoft Word文件檔案對於希望自動化任務或將文件處理整合到其應用程式中的開發者來說是至關重要的。 要能夠以程式方式在C#中處理Microsoft Word文件,有很多文件程式庫可用。 其中一個這樣的程式庫是IronWord,這是Iron Software的一個強大C# Word DOCX程式庫,它簡化了在.NET應用程式中處理Word文件。
在本文中,我們將探索強大的IronWord - The C# Library,其特點,打開Word文件集合,並從中讀取資料。
如何在C#中打開Word文件集合
- 在Visual Studio中建立一個控制台應用程式
- 安裝IronWord C# DOCX程式庫
- 使用WordDocument類打開Word文件
- 使用Paragraph類遍歷每個段落
- 對每個段落運行TextRuns
- 使用SaveAs方法顯示內容或儲存
IronWord - The C# DOCX Library
IronWord是由Iron Software開發的功能豐富的C# Word DOCX程式庫。 它提供了使用者友好的API,讓開發者可以輕鬆地在其.NET應用程式中處理Word文件。 無論您是建立新Word文件、編輯現有文件,還是提取內容,IronWord都提供了一套全面的工具來簡化這一過程。
功能集
1. 相容性和跨平台支持
IronWord設計得非常通用,支持各種.NET版本,包括.NET 8、7、6、Framework、Core和Azure。 開發者可以在不同平台上使用它,如Windows、Linux、macOS、iOS、Android,使其適應於廣泛的.NET應用程式開發場景。
2. 文件操作
IronWord的功能不僅僅限於簡單的文件建立。 它允許複雜的文件操作,包括文字和段落格式設定、圖片和形狀整合、表格建立等。 這種多樣性使IronWord適用於需要對文件結構和內容進行精確控制的各種應用。
3. 無需依賴Microsoft Office
IronWord的一個顯著特點是其獨立於Microsoft Office安裝或Word Interop的特性。這意味著不需要Word應用程式。 開發人員可以利用其功能而無需擔心額外的依賴,確保更流暢和更高效的開發過程。
4. 容易使用
該程式庫具有使用者友好的API,允許開發人員無縫整合Word文件處理功能到他們的.NET專案中。 此外,IronWord不需安裝Microsoft Office或Word Interop,確保無痛的開發體驗。
先決條件
在深入瞭解IronWord之前,請確保您已具備以下先決條件:
設置環境
首先,打開Visual Studio,您將看到歡迎螢幕。
1. 建立新的.NET Framework控制臺應用程式
點擊"建立新專案"。 搜尋"Console App (.NET Framework)",從列表中選擇它,然後點擊"下一步"。 為您的專案命名,然後點擊"建立"。 Visual Studio將設置一個新的.NET Framework控制台應用程式,並提供一個包含Main方法作為入口點的基本模板。

2. 使用NuGet程式包管理器安裝IronWord
在Visual Studio中,導航到"工具"選單,選擇"NuGet程式包管理器",然後選擇"管理解決方案的NuGet程式包"。 在NuGet窗口中,移動到"瀏覽"選項卡,在搜索框中輸入"IronWord",然後按下Enter。 從結果中選擇程式包,確保右側選中您的控制台應用程式專案,然後點擊"安裝"。 這將為您在C#應用程式中使用IronWord新增必要的引用。 現在您已準備好開始使用IronWord處理Word文件。

3. 在程式碼中新增IronWord引用:
在您的C#程式碼文件中,在Program.cs文件中新增如下使用語句來引用IronWord:
using IronWord;
using IronWord;
Imports IronWord
開啟Word文件並讀取內容的步驟
現在我們的專案已設置完成,請按照以下步驟通過IronWord打開Word文件並讀取其內容:
- 載入現有文件:
// Load an existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
// Load an existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
' Load an existing Word document file
Dim doc As New WordDocument("existing_document.docx")
在本步驟中,我們從IronWord程式庫建立WordDocument類的實例。 我們使用接受現有輸入Word文件路徑的建構函式進行初始化(existing_document.docx)。 這將初始化doc物件,表示從輸入文件載入的Word文件。
輸入文件:

- 讀取和操作內容:
以下程式碼有助於從打開的文件文件中讀取文字內容:
// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (TextRun textRun in paragraph.TextRuns)
{
// Access the text content of each text run
string content = textRun.Text;
// Display content on the console
Console.WriteLine(content);
}
}
// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (TextRun textRun in paragraph.TextRuns)
{
// Access the text content of each text run
string content = textRun.Text;
// Display content on the console
Console.WriteLine(content);
}
}
' Access paragraphs and text runs
For Each paragraph As Paragraph In doc.Paragraphs
For Each textRun As TextRun In paragraph.TextRuns
' Access the text content of each text run
Dim content As String = textRun.Text
' Display content on the console
Console.WriteLine(content)
Next textRun
Next paragraph
此處,我們迭代載入的Word文件中的段落和文字運行(doc)。 foreach迴圈允許我們遍歷每個段落,並在段落中巢狀遍歷每個文字運行。 對於每個textRun.Text存取文字內容。 這是您可以進行所需操作的位置,例如提取資訊或以程式方式修改文字內容。
- 顯示內容和輸出:
// Display contents
Console.WriteLine(content);
// Display contents
Console.WriteLine(content);
' Display contents
Console.WriteLine(content)
在前一個步驟的第二個foreach迴圈中,我們在控制台輸出螢幕上顯示可見的單詞內容。 我們還可以將打開的文件的某些部分保存為新文件:
// Method to save changes to the document
doc.SaveAs("modified_document.docx");
// Method to save changes to the document
doc.SaveAs("modified_document.docx");
' Method to save changes to the document
doc.SaveAs("modified_document.docx")
完整的程式碼如下所示:
using IronWord;
using IronWord.Models;
namespace IronWordExample
{
// Main program class
class Program
{
// Main method - Entry point of the application
public static void Main(string[] args)
{
// Load existing Word doc file
WordDocument doc = new WordDocument("existing_document.docx");
// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (TextRun textRun in paragraph.TextRuns)
{
// Access text content
string content = textRun.Text;
// Display Contents
Console.WriteLine(content);
}
}
// Save changes to the document
doc.SaveAs("modified_document.docx");
}
}
}
using IronWord;
using IronWord.Models;
namespace IronWordExample
{
// Main program class
class Program
{
// Main method - Entry point of the application
public static void Main(string[] args)
{
// Load existing Word doc file
WordDocument doc = new WordDocument("existing_document.docx");
// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
foreach (TextRun textRun in paragraph.TextRuns)
{
// Access text content
string content = textRun.Text;
// Display Contents
Console.WriteLine(content);
}
}
// Save changes to the document
doc.SaveAs("modified_document.docx");
}
}
}
Imports IronWord
Imports IronWord.Models
Namespace IronWordExample
' Main program class
Friend Class Program
' Main method - Entry point of the application
Public Shared Sub Main(ByVal args() As String)
' Load existing Word doc file
Dim doc As New WordDocument("existing_document.docx")
' Access paragraphs and text runs
For Each paragraph As Paragraph In doc.Paragraphs
For Each textRun As TextRun In paragraph.TextRuns
' Access text content
Dim content As String = textRun.Text
' Display Contents
Console.WriteLine(content)
Next textRun
Next paragraph
' Save changes to the document
doc.SaveAs("modified_document.docx")
End Sub
End Class
End Namespace

要探索IronWord可以執行的更多功能,請存取此程式碼範例頁面。
結論
在本文中,我們探討了IronWord的功能庫,這是一個強大的C# Word DOCX程式庫,簡化了以程式方式打開和操作Word文件的過程。 通過提供豐富的功能集並消除對外部軟體的依賴,IronWord使開發者能夠無縫地將文件處理整合到其.NET應用程式中。 無論您是在自動化與文件相關的任務還是提升體驗,IronWord在您.NET工具包中證明是一個有價值的工具。
要了解更多並將IronWord整合到您的新應用專案中,請存取文件頁。
IronWord提供免費試用以測試其完整功能。 這可以幫助您在購買之前做出明智的決定。 其Lite授權從$999開始,詳細資訊可以在此許可頁面上找到。
從這裡免費試用IronWord。
常見問題
如何在C#中打開Microsoft Word文件?
您可以使用IronWord程式庫在C#中打開Microsoft Word文件。首先,在Visual Studio中設置控制臺應用程式,通過NuGet包管理器安裝IronWord,然後使用WordDocument類載入您的Word文件。
使用IronWord進行文件處理有哪些好處?
IronWord提供廣泛的文件操作功能,支持多個.NET版本和平臺,且不需要Microsoft Office或Word Interop。它允許使用者高效地格式化文字、整合圖片和建立表格。
如何使用C#操作Word文件的內容?
使用IronWord,您可以通過讀取段落、迭代文字運行並修改它們來操作Word文件內容。該程式庫提供了文字格式化和無縫整合圖片的方法。
IronWord需要安裝任何附加軟體嗎?
不,IronWord不需要像Microsoft Office或Word Interop這樣的附加軟體安裝。它是一個獨立的程式庫,可以在您的.NET應用程式中獨立運作。
如何在C#中保存對Word文件的修改?
您可以通過利用IronWord的SaveAs方法保存對Word文件的修改。這樣可以將編輯過的文件導出為一個新文件。
IronWord可以在什麼平臺上使用?
IronWord支持包括Windows、Linux、macOS、iOS和Android在內的多種平臺,因為它與.NET 8、7、6、Framework、Core和Azure相容。
如何在我的專案中安裝IronWord?
要安裝IronWord,請在Visual Studio中使用NuGet包管理器。搜索“IronWord”,並將其新增到您的專案中以開始處理Word文件。
在購買授權之前可以試用IronWord嗎?
是的,IronWord提供免費試用,讓您測試其功能。您可以在Iron Software網站上查看更多關於試用和授權選項的資訊。
在哪里可以找到更多使用IronWord的範例?
如需更多程式碼範例和使用IronWord的詳細文件,您可以存取Iron Software網站,該網站提供全面的資源幫助您入門。



