使用IRONWORD

C# 打開Word文件

發佈 2023年11月13日
分享:

Word 應用文檔在專業和個人溝通的各個方面都是不可或缺的。 能夠以編程方式操作和互動 Microsoft Word 文件對於開發者來說是至關重要的,特別是那些想自動化任務或將文檔處理集成到他們的應用中。 要能夠在 C# 中以程序化方式處理 Microsoft Word 文件,有許多文檔庫可供選擇。 其中一個庫是 IronWord,這是 Iron Software 開發的一個強大的 C# Word DOCX 庫,它簡化了在 .NET 應用中處理 Word 文件的過程。

本文將探討功能強大的 IronWord - The C# Library,其功能,打開 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 主控台應用程式

要開始使用 IronWord,請建立一個新的 Visual Studio 專案。這可以是主控台應用程式、Windows Forms 應用程式或任何其他符合您需求的專案類型。

請按照以下步驟來建立一個 Visual Studio 主控台應用程式:

  1. 打開 Visual Studio:在您的電腦上啟動 Visual Studio。

  2. 建立一個新專案

    • 點擊「建立新專案」

    • 在「C#」類別下選擇「主控台應用程式」。

    • 為您的專案輸入名稱並選擇位置。

    新專案配置

    • 選擇最新的 .NET Framework 8.0。IronWord 完全支援 .NET 8, 7, 6, Framework, Core。

    目標框架

    • 點擊“Create”創建專案。
  3. 通過NuGet安裝IronWord:
    • 打開NuGet套件管理器控制台 (工具 > NuGet 套件管理器 > 套件管理主控台).
    • 執行以下程式碼命令來安裝 IronWord:
    Install-Package IronWord
  • 或者,您可以從 NuGet 套件管理器下載並安裝 (右鍵 解決方案總管 -> 管理解決方案的 NuGet 套件管理員)

    在 NuGet 瀏覽視窗中,搜尋 IronWord 並安裝:

    IronWord

  1. 在程式碼中引用 IronWord:在您的 C# 程式檔案中,在 Program.cs 文件中添加以下 using 語句來引用 IronWord:
using IronWord;
using IronWord;
Imports IronWord
VB   C#

打開Word文件並讀取內容的步驟

現在我們的項目已經設置好,請按照以下步驟使用IronWord打開Word文件並讀取其內容:

  1. 加載現有文檔:
// Load existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
// Load existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
' Load existing Word document file
Dim doc As New WordDocument("existing_document.docx")
VB   C#

在此步驟中,我們從 IronWord 庫中創建一個 WordDocument 類的實例。我們使用一個包含現有輸入 Word 文件路徑的構造函數。 (existing_document.docx)這將初始化doc對象發送者,表示從輸入文件名加載的Word文檔。

輸入文件:

輸入

  1. 閱讀和操作內容:

以下程式碼有助於從打開的文檔檔案中讀取文字內容:

// 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);
    }
}
// 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);
    }
}
' 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
VB   C#

在這裡,我們會迭代載入的 Word 文件中的段落和文字執行 (文件). foreach 迴圈允許我們遍歷每一個段落,並在其中嵌套每個文字運行。對於每個 textRun,我們可以使用 textRun.Text 訪問文本內容。在這個階段,你可以執行任何所需的操作,例如提取信息或程式化地修改文本內容。

  1. 顯示內容和輸出:
// Display contents
Console.WriteLine(content);
// Display contents
Console.WriteLine(content);
' Display contents
Console.WriteLine(content)
VB   C#

在上一個步驟的第二個 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")
VB   C#

完整的程式碼如下:

using IronWord;
using IronWord.Models;

namespace IronWordExample
{
    class Program
    {
        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
{
    class Program
    {
        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
	Friend Class Program
		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
VB   C#

輸出 若要探索更多 IronWord 的功能,請訪問這個 程式碼範例 頁面。

結論

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

要了解更多內容並開始將 IronWord 集成到您的新應用程序項目中,請訪問 文檔頁面鐵Word(IronWord)提供了一個 免費試用 以測試其完整功能。這有助於您在購買前進行明智的決策。其 Lite License 從 $749 起,更多詳情請參閱此 授權頁面嘗試免費使用 IronWord,來自 這裡.

< 上一頁
如何在C#中將Word轉換為PDF
下一個 >
C# 列印 Word(開發者教學)

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 5,614 查看許可證 >