跳過到頁腳內容
使用 IRONWORD
教學 如何在 C# 中開啟 Word 文件

C# 開啟 Word 文件

Word應用程式文件是專業和個人溝通各個方面不可或缺的一部分。 對於希望自動化任務或將文件處理整合到其應用程式中的開發人員來說,能夠以程式設計方式操作和互動 Microsoft Word 文件文件至關重要。 要在 C# 中以程式設計方式處理 Microsoft Word 文檔,可以使用許多文檔庫。 IronWord就是這樣一個函式庫,它是 Iron Software 開發的一款強大的 C# Word DOCX 函式庫,可以簡化在 .NET 應用程式中處理 Word 文件的操作。

在本文中,我們將探索功能強大的IronWord - C# 庫,包括其特性、開啟 Word 文件集合以及從中讀取資料。

如何在 C# 中開啟 Word 文件集合

  1. 在 Visual Studio 中建立一個控制台應用程式
  2. 安裝 IronWord C# DOCX 函式庫
  3. 使用WordDocument類別開啟 Word 文檔
  4. 使用Paragraph類別遍歷每個段落。
  5. 對每個段落運行TextRuns
  6. 顯示內容或使用SaveAs方法儲存

IronWord - C# DOCX 庫

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 :確保您已安裝並可正常執行 Visual Studio,這是一個受歡迎的 .NET 開發整合開發環境。 你可以從這裡下載。
  • IronWord :您需要下載庫才能使用其功能。 您可以直接從這裡下載 NuGet 套件。

設定環境

首先,打開Visual Studio ,您將看到歡迎畫面。

1. 建立一個新的 .NET Framework 控制台應用程式

點擊"建立新項目"。搜尋"控制台應用程式 (.NET Framework)",從清單中選擇它,然後點擊"下一步"。為項目命名,然後點選"建立"。 Visual Studio 將使用基本範本建立新的 .NET Framework 控制台應用程序,其中包含一個作為入口點的 Main 方法。

新專案配置

2. 使用 NuGet 套件管理器安裝 IronWord

在 Visual Studio 中,導覽至"工具"選單,選擇"NuGet 套件管理員",然後選擇"管理解決方案的 NuGet 套件"。在 NuGet 視窗中,前往"瀏覽"選項卡,在搜尋框中鍵入"IronWord",然後按 Enter 鍵。 從結果中選擇該軟體包,確保右側已選取您的控制台應用程式項目,然後按一下"安裝"。這將會加入在 C# 應用程式中使用 IronWord 所需的參考。 現在您就可以開始使用 IronWord 處理 Word 文件了。

IronWord

3. 在程式碼中加入對 IronWord 的引用:

在您的 C# 程式碼檔案中,在 Program.cs 檔案中加入以下 using 語句以引用 IronWord:

using IronWord;
using IronWord;
Imports IronWord
$vbLabelText   $csharpLabel

開啟Word文件並閱讀內容的步驟

專案設定完成後,請依照下列步驟使用 IronWord 開啟 Word 文件並讀取其內容:

  1. 載入現有文檔:
// 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")
$vbLabelText   $csharpLabel

在此步驟中,我們將從 IronWord 庫中建立一個WordDocument類別的實例。 我們使用建構函數,該構造函數接受現有輸入 Word 文件的路徑( existing_document.docx )。 這將初始化doc對象,該對象表示從輸入文件載入的 Word 文件。

輸入檔:

Input

  1. 閱讀和處理內容:

以下程式碼用於讀取已開啟文件文件中的文字內容:

// 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
$vbLabelText   $csharpLabel

在這裡,我們遍歷已載入的 Word 文件 ( doc ) 中的段落和文字行。 foreach循環讓我們可以遍歷每個段落,以及嵌套在其中的每個文字行。 對於每個textRun ,我們可以使用textRun.Text存取文字內容。 在這裡,您可以執行任何所需的操作,例如以程式設計方式提取資訊或修改文字內容。

  1. 顯示內容和輸出:
// Display contents
Console.WriteLine(content);
// Display contents
Console.WriteLine(content);
' Display contents
Console.WriteLine(content)
$vbLabelText   $csharpLabel

在上一個步驟的第二個 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")
$vbLabelText   $csharpLabel

完整的程式碼如下:

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
$vbLabelText   $csharpLabel

Output

若要了解 IronWord 的更多功能,請造訪此程式碼範例頁面。

結論

在本文中,我們探討了 IronWord 的功能,這是一個強大的 C# Word DOCX 程式庫,可以簡化以程式設計方式開啟和操作 Word 文件的過程。 IronWord 提供豐富的功能集並消除對外部軟體的依賴,使開發人員能夠將文件處理無縫整合到他們的 .NET 應用程式中。 無論您是自動化文件相關任務還是增強使用者體驗,IronWord 都證明是您 .NET 工具包中一個有價值的工具。

若要了解更多並開始將 IronWord 整合到您的新應用程式專案中,請造訪文件頁面

IronWord 提供免費試用版,供用戶測試其完整功能。 這可以幫助你在購買前做出明智的決定。 其 Lite 授權起價為$799 ,更多詳情請參閱此授權頁面

這裡免費試用IronWord。

常見問題解答

如何在 C# 中開啟 Microsoft Word 文件?

您可以使用 IronWord 函式庫在 C# 中開啟 Microsoft Word 文件。首先,在 Visual Studio 中設定一個主控台應用程式,透過 NuGet 套件管理員安裝 IronWord,並使用 WordDocument 類載入您的 Word 檔案。

使用 IronWord 處理文件有哪些好處?

IronWord for .NET 提供廣泛的文件操作功能,支援多種 .NET 版本與平台,且不需要 Microsoft Office 或 Word Interop。它能讓使用者有效率地格式化文字、整合圖片和建立表格。

如何使用 C# 操作 Word 文件的內容?

使用 IronWord,您可以透過讀取段落、迭代文字運行和修改來操作 Word 文件內容。該函式庫提供了格式化文字和無縫整合圖片的方法。

IronWord 是否需要安裝任何其他軟體?

不,IronWord 不需要安裝其他軟體,例如 Microsoft Office 或 Word Interop。它是一個獨立的函式庫,可在您的 .NET 應用程式中獨立運作。

如何保存在 C# 中對 Word 文檔所做的修改?

您可以利用 SaveAs 方法儲存使用 IronWord 對 Word 文件所做的修改。這可讓您將已編輯的文件匯出為新檔案。

IronWord 可在哪些平台上使用?

IronWord 支援多種平台,包括 Windows、Linux、macOS、iOS 和 Android,因為它相容於 .NET 8、7、6、Framework、Core 和 Azure。

如何在我的專案中安裝 IronWord?

若要安裝 IronWord,請使用 Visual Studio 中的 NuGet Package Manager。搜尋「IronWord」,並將其新增至您的專案,即可開始處理 Word 文件。

在購買授權之前,我可以先試用 IronWord 嗎?

是的,IronWord 提供免費試用版,可讓您測試其功能。您可以在 Iron Software 網站探索更多關於試用版和授權選項的資訊。

我在哪裡可以找到更多使用 IronWord 的範例?

如需更多關於使用 IronWord 的程式碼範例和詳細說明文件,您可以造訪 Iron Software 網站,該網站提供全面的資源,可協助您開始使用。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。