跳過到頁腳內容
使用 IRONWORD

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

Microsoft Word files, commonly known as DOCX files, are integral to document creation and communication in various industries. The ability to programmatically generate and manipulate Word documents is crucial for developers in Visual Basic.

In this article, we'll explore the importance of creating Microsoft Word files in C# applications and introduce the IronWord library as a powerful tool to generate Word Documents programmatically.

How to Create Word Document Programmatically in VB .NET

  1. Create a Visual Basic project in Visual Studio
  2. Install the IronWord Library from the NuGet Package Manager
  3. Create a TextRun and Paragraph
  4. Create a WordDocument ClassObject
  5. Add TextRun and Paragraph to WordDocument Object
  6. Export as a DOCX file using the SaveAs method

IronWord Library

IronWord is a feature-rich C# DOCX library developed by IronSoftware. It empowers developers to build, edit, and export Word documents without needing Microsoft Office or Word Interop. Supporting a range of .NET versions, IronWord provides a versatile solution for working with Word files in diverse project types and environments.

How to Create Word Document Programmatically in VB .NET: Figure 1 - IronWord webpage

Key Features:

IronWord offers a comprehensive set of features, making it a go-to library for Word document manipulation in C#. Some key features include:

  • Cross-Platform Support: Designed for C#, F#, and VB.NET, IronWord runs on various .NET versions, including 8, 7, 6, 5, Core, Standard, or Framework. It also caters to web, mobile, desktop, and console applications.

  • Document Structure Manipulation: IronWord allows developers to read and edit Word documents, extract text and images, customize page setup, and add paragraphs, text runs, images, shapes, and tables.

  • Styling and Formatting: The library supports styling options such as font family and size, color, bold, italic, underline, superscript, subscript, alignments, background color, and bullet/numbering lists.

  • Cross-Platform Compatibility: IronWord seamlessly works across different operating systems, including Windows, Linux, macOS, iOS, and Android.

Steps to Create a C# Console Project in Visual Studio

  1. Open Visual Studio.
  2. Create a new C# Console .NET Application.
  3. Name your project and choose a location.

How to Create Word Document Programmatically in VB .NET: Figure 2 - Configuring the project name and file path

  1. From Additional Information, select the latest version of the .NET Framework. IronWord supports the latest 8.0 .NET Core Framework.
  2. Click "Create" to generate the project.

Installing IronWord

To incorporate IronWord into your project to create a simple Word document, follow these steps:

Using the NuGet Package Manager

  1. Right-click on your project in Solution Explorer.
  2. Select "Manage NuGet Packages for Solution."
  3. Search for "IronWord" in the NuGet Package Manager browse tab.

How to Create Word Document Programmatically in VB .NET: Figure 3 - Installing IronWord in the NuGet package manager

  1. Click "Install" to add the IronWord library to your project.

Using Package Manager Console

Run the following command in the Package Manager Console:

Install-Package IronWord

Creating a Word Document in VB.NET

Let's go through a simple example to demonstrate how to create a Word document using IronWord in VB.NET:

Imports IronWord
Imports IronWord.Models

Module Program
    Sub Main()
        ' Create a text run containing the desired content
        Dim textRun As New TextRun("Sample text")

        ' Create a paragraph and add the text run to it
        Dim paragraph As New Paragraph()
        paragraph.AddTextRun(textRun)

        ' Create a new Word document containing the paragraph
        Dim document As New WordDocument(paragraph)

        ' Export the document as a DOCX file with the name "document.docx"
        document.SaveAs("document.docx")
    End Sub
End Module
Imports IronWord
Imports IronWord.Models

Module Program
    Sub Main()
        ' Create a text run containing the desired content
        Dim textRun As New TextRun("Sample text")

        ' Create a paragraph and add the text run to it
        Dim paragraph As New Paragraph()
        paragraph.AddTextRun(textRun)

        ' Create a new Word document containing the paragraph
        Dim document As New WordDocument(paragraph)

        ' Export the document as a DOCX file with the name "document.docx"
        document.SaveAs("document.docx")
    End Sub
End Module
VB .NET

Code Explanation

Let's break down the key components of this VB.NET code for creating a Word Document:

Importing the IronWord Namespace:

Imports IronWord
Imports IronWord.Models
Imports IronWord
Imports IronWord.Models
VB .NET

These lines import the necessary namespaces from the IronWord library to use the classes and Word document model in your VB.NET code.

Creating a TextRun and 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)
VB .NET

Here, a TextRun is created with the content "Sample text," and a Paragraph is created to hold the TextRun. This sets up the document with the content structure of the Word document.

Creating a Word Document:

The following code creates a new document object and adds a text paragraph to it:

Dim document As New WordDocument(paragraph)
Dim document As New WordDocument(paragraph)
VB .NET

An instance of the WordDocument class is created, and the Paragraph is passed to its constructor. This initializes the Word document with the specified content.

Exporting the Document:

document.SaveAs("document.docx")
document.SaveAs("document.docx")
VB .NET

Finally, the SaveAs method is used to export the Word document as a DOCX file with the filename "document.docx."

Output:

Upon running the program, a Word File named "document.docx" will be generated in the project directory. Open this document to view the added content.

How to Create Word Document Programmatically in VB .NET: Figure 4 - Outputted PDF document from the previous code

IronWord provides simplicity and allows one to edit and manipulate specific paragraphs. To get more information on the methods involved in the document class, please visit this code examples page.

Conclusion

In this article, we explored the importance of creating Microsoft Word files in C# applications and introduced the IronWord library as a powerful tool for achieving this task. With its rich feature set, cross-platform support, and ease of integration into Visual Studio projects, IronWord simplifies the process of handling Word documents programmatically. Developers can leverage IronWord to enhance document processing capabilities and streamline creating and manipulating Word files in their C# applications.

For more detailed information about IronWord VB.NET and its capabilities, please visit its documentation page on IronWord's official website.

IronWord is free for development purposes and can be licensed to test out its complete functionality in commercial mode before deciding. Download the library from here and give it a try.

In this article, we explored the importance of creating Microsoft Word files in C# applications and introduced the IronWord library as a powerful tool for achieving this task. With its rich feature set, cross-platform support, and ease of integration into Visual Studio projects, IronWord simplifies the process of handling Word documents programmatically. Developers can leverage IronWord to enhance document processing capabilities and streamline creating and manipulating Word files in their C# applications.

常見問題解答

如何使用VB.NET程式化建立Word文件?

要使用 VB.NET 程式化建立 Word 文件,請在 Visual Studio 中設置 Visual Basic 專案,從 NuGet 包管理器安裝 IronWord 程式庫,創建TextRunParagraph對象,然後使用WordDocument類通過SaveAs方法導出文件為 DOCX 檔案。

我可以使用這個程式庫在沒有 Microsoft Office 的情況下操作 Word 文件嗎?

是的,您可以使用 IronWord 程式庫在沒有 Microsoft Office 的情況下操作 Word 文件,該程式庫獨立於 Microsoft Office 或 Word Interop 運行。

使用此程式庫進行 Word 文件處理有何優勢?

IronWord 提供跨平台支持、文件結構操作、設計和格式選項以及與各種 .NET 版本和操作系統的兼容性等優勢。

如何通過 NuGet 套件管理器安裝用於創建Word文件的程序庫?

您可以使用 Visual Studio 中的 NuGet 套件管理器通過執行命令:Install-Package IronWord,安裝用於創建 Word 文件的 IronWord 程式庫。

該程式庫適合商業用途嗎?

IronWord 可免費用於開發目的,並提供在商業模式下全面測試其功能的選擇,然後再做出許可決定。

哪些類型的應用程式可以利用這個 Word 文件程式庫?

需要程式化生成和操作 Word 文件的應用程式(如網絡、移動、桌面和控制台應用程式)可以利用 IronWord 程式庫。

我可以在哪裡找到這個 Word 文件程式庫的詳細文檔?

IronWord 功能的詳細文檔和其他信息可以在 Iron Software 提供的IronWord 官方文檔頁面上找到。

為什麼程式化生成 Word 文件很重要?

程式化生成 Word 文件對於自動化文件創建和操作非常重要,提升了各種應用和行業的生產力和效率。

在 Visual Studio 中設置 Word 文件專案的基本步驟有哪些?

基本步驟包括創建Visual Basic專案、通過NuGet套件管理器安裝IronWord,並將TextRunParagraph對象集成到WordDocument中以導出為DOCX文件。

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