使用IRONWORD

如何在 VB .NET 中程式化地創建 Word 文件

發佈 2024年3月12日
分享:

Microsoft Word 檔案,通常稱為 DOCX 檔案,是在各行各業中進行文件創建和交流的重要組成部分。 程序性生成和操作 Word 文件的能力對於使用 Visual Basic 的開發人員來說至關重要。

在本文中,我們將探討在 C# 應用程式中創建 Microsoft Word 文件的重要性,並介紹IronWord將該庫作為一個強大的工具來程式化地生成 Word 文件。

如何在 VB .NET 中程式化地創建 Word 文件

  1. 在 Visual Studio 中創建 Visual Basic 專案

  2. 從 NuGet 套件管理器安裝 IronWord 程式庫

  3. 建立一個TextRunParagraph

  4. 創建一個 WordDocument 類別對象

  5. TextRunParagraph 添加到 WordDocument 對象中

  6. 使用 SaveAS 方法匯出為 DOCX 文件

IronWord 庫

IronWord是一個功能豐富的 C# DOCX 函式庫,由 IronSoftware 開發。 它使開發者能夠建構、編輯和匯出 Word 文件,而不需要 Microsoft Office 或 Word Interop。IronWord 支援多個 .NET 版本,為在各種專案類型和環境中處理 Word 文件提供了靈活解決方案。

如何在VB .NET中以程式方式建立Word文件:圖1 - IronWord網頁

主要特點:

IronWord 提供了全面的功能集,是用於在 C# 中操控 Word 文件的首選庫。 一些主要功能包括:

跨平台支援: IronWord 專為 C#、F# 和 VB.NET 設計,可在多種 .NET 版本上運行,包括 8、7、6、5、Core、Standard 或 Framework。 它還適用於網路、行動裝置、桌面和主控台的應用程式。

文件結構操作: IronWord 允許開發人員讀取和編輯 Word 文件,提取文本和圖像,自定義頁面設置,並添加段落、文字運行、圖像、形狀和表格。

樣式和格式化:該庫支持樣式選項,如字體系列和大小、顏色、粗體、斜體、下劃線、上標、下標、對齊、背景色和項目符號/編號列表。

跨平台相容性: IronWord 可在不同的作業系統上無縫運作,包括 Windows、Linux、macOS、iOS 和 Android。

在 Visual Studio 中建立 C# 控制台專案的步驟

  1. 打開 Visual Studio。

  2. 創建一個新的 C# Console .NET 應用程式。

    1. 為您的專案命名並選擇位置。

    如何在 VB .NET 中以程式方式建立 Word 文件:圖 2 - 配置專案名稱和檔案路徑

  3. 從「其他資訊」中選擇最新版本的 .NET Framework。 IronWord 支援最新的 8.0 .NET Core 框架。

  4. 點擊「建立」以生成專案。

安裝 IronWord

要將IronWord整合到您的專案中以建立一個簡單的Word文件,請按照以下步驟操作:

使用 NuGet 套件管理器

  1. 在方案總管中右鍵點擊您的項目。

  2. 選擇「管理方案的 NuGet 套件」。

    1. 在 NuGet 封裝管理員的瀏覽標籤中搜尋 "IronWord"。

    如何以 VB .NET 程式碼創建 Word 文件:圖 3 - 在 NuGet 套件管理器中安裝 IronWord

  3. 點擊「安裝」以將 IronWord 庫添加到您的專案中。

使用套件管理器控制台

在套件管理器控制台中運行以下命令:

Install-Package IronWord

在 VB.NET 中建立 Word 文件

讓我們通過一個簡單的例子來展示如何在 VB.NET 中使用 IronWord 創建一個 Word 文檔:

Imports IronWord
Imports IronWord.Models
Module Program
    Sub Main()
        ' Create a textrun
        Dim textRun As New TextRun("Sample text")
        ' Create a paragraph and add the textrun
        Dim paragraph As New Paragraph()
        paragraph.AddTextRun(textRun)
        ' Create a new Word document using the paragraph
        Dim document As New WordDocument(paragraph)
        ' Export the document as a docx file
        document.SaveAs("document.docx")
    End Sub
End Module
Imports IronWord
Imports IronWord.Models
Module Program
    Sub Main()
        ' Create a textrun
        Dim textRun As New TextRun("Sample text")
        ' Create a paragraph and add the textrun
        Dim paragraph As New Paragraph()
        paragraph.AddTextRun(textRun)
        ' Create a new Word document using the paragraph
        Dim document As New WordDocument(paragraph)
        ' Export the document as a docx file
        document.SaveAs("document.docx")
    End Sub
End Module
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Imports IronWord @Imports IronWord.Models Module Program @Sub Main() ' Create a textrun @Dim textRun @As @New TextRun("Sample text") ' Create a paragraph @and add the textrun @Dim paragraph @As @New Paragraph() paragraph.AddTextRun(textRun) ' Create a New Word document using the paragraph @Dim document @As @New WordDocument(paragraph) ' Export the TryCast(document, a) docx file document.SaveAs("document.docx") @End @Sub @End Module
VB   C#

程式碼解釋

讓我們分解一下這段 VB.NET 代碼的關鍵組成部分創建 Word 文件:

導入 IronWord 命名空間:

Imports IronWord
Imports IronWord.Models
Imports IronWord
Imports IronWord.Models
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Imports IronWord @Imports IronWord.Models
VB   C#

這些行從 IronWord 函式庫中匯入必要的命名空間,以便在您的 VB.NET 代碼中使用類和 Word 文件模型。

創建 TextRun 和 Paragraph:

Dim textRun As New TextRun("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
Dim textRun As New TextRun("Sample text")
Dim paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Dim textRun @As @New TextRun("Sample text") @Dim paragraph @As @New Paragraph() paragraph.AddTextRun(textRun)
VB   C#

在這裡,創建了一個內容為「Sample text」的TextRun,並創建了一個Paragraph來容納該TextRun。 這將以 Word 文件的內容結構設置模板文件。

建立 Word 文件:

以下程式碼建立一個新的文件對象並向其中添加一個文字段落:

Dim document As New WordDocument(paragraph)
Dim document As New WordDocument(paragraph)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Dim document @As @New WordDocument(paragraph)
VB   C#

WordDocument 類別的一個實例被創建,並將 Paragraph 傳遞給其構造函數。 這會以指定的內容初始化 Word 文件。

匯出文件:

document.SaveAs("document.docx")
document.SaveAs("document.docx")
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'document.SaveAs("document.docx")
VB   C#

最後,使用 SaveAs 方法將 Word 文件匯出為名為 "document.docx" 的 DOCX 檔案。

輸出:

執行程式後,一個名為 "output.docx" 的 Word 文件將在專案目錄中生成。 開啟此文件以查看新增內容。

如何在VB .NET中以程式方式建立Word文件:圖4 - 幾段代碼生成的PDF文件输出

IronWord 提供簡單性,並允許用戶編輯和操作特定段落。要獲取有關文件類中方法的更多信息,請訪問此連結代碼範例頁面。

結論

在本文中,我們探討了在 C# 應用程式中建立 Microsoft Word 文件的重要性,並介紹了IronWord資料庫作為實現此任務的強大工具。 IronWord 擁有豐富的功能集、跨平台支持以及易於整合到 Visual Studio 專案中,簡化了以程式方式處理 Word 文件的過程。 開發人員可以利用 IronWord 來增強文件處理功能,並簡化其 C# 應用程式中 Word 檔案的建立和操作。

如需有關 IronWord VB.NET 及其功能的更多詳細資訊,請造訪其文檔IronWord 官方網站的頁面。

IronWord 免費供開發用途並且可以授權在決定之前,先以商業模式測試其完整功能。 從下載該庫這裡並試試看。

在本文中,我們探討了在 C# 應用程式中建立 Microsoft Word 文件的重要性,並介紹了 IronWord 程式庫作為實現這一任務的強大工具。 IronWord 擁有豐富的功能集、跨平台支持以及易於整合到 Visual Studio 專案中,簡化了以程式方式處理 Word 文件的過程。 開發人員可以利用 IronWord 來增強文件處理功能,並簡化其 C# 應用程式中 Word 檔案的建立和操作。

< 上一頁
如何在C#中建立DOCX文件
下一個 >
如何在 C# 中創建無需 Office 互操作性的 Word 文件