使用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 Library

IronWord 是一個功能豐富的C# DOCX庫,由IronSoftware開發。它使開發人員可以在不需要Microsoft Office或Word Interop的情況下構建、編輯和導出Word文檔。支持各種.NET版本,IronWord在不同的項目類型和環境中提供了處理Word文件的多功能解決方案。

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

主要特點:

IronWord 提供全面的功能集,使其成為在 C# 中操作 Word 文檔的首選庫。部分主要功能包括:

跨平台支持: 專為 C#、F# 和 VB.NET 設計,IronWord 運行於多種 .NET 版本,包括 8、7、6、5、Core、Standard 或 Framework。它還適用於網頁、移動設備、桌面和控制台應用程式。

文檔結構操作: IronWord 允許開發人員讀取和編輯 Word 文檔、提取文本和圖像、自定頁面設置,並新增段落、文本段、圖像、形狀和表格。

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

跨平台兼容性: IronWord 無縫運行於不同操作系統,包括 Windows、Linux、macOS、iOS 和 Android。

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

  1. 打開 Visual Studio。

  2. 建立一個新的 C# 主控台 .NET 應用程式。

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

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

  1. 從其他資訊中,選擇最新版本的 .NET Framework。 IronWord 支援最新的 8.0 .NET Core Framework。

  2. 點擊「建立」來生成專案。

安裝 IronWord

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

使用 NuGet 套件管理器

  1. 在解決方案總管中右鍵單擊您的專案。

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

  3. 在 NuGet 套件管理器的瀏覽標籤中搜尋「IronWord」。

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

  1. 點擊「安裝」將 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,並創建了一個保存TextRunParagraph。這設置了具有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 庫作為達成此任務的強大工具。憑其豐富的功能集、跨平台支持以及輕鬆集成到 Visual Studio 項目的特性,IronWord 簡化了以編程方式處理 Word 文件的過程。開發人員可以利用 IronWord 提高文檔處理能力,並簡化在 C# 應用程序中創建和操作 Word 文件的過程。

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

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

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