使用 IRONWORD 如何在 VB .NET 中编程创建 Word 文档 Jordi Bardia 已更新:六月 22, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 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 Create a Visual Basic project in Visual Studio Install the IronWord Library from the NuGet Package Manager Create a TextRun and Paragraph Create a WordDocument ClassObject Add TextRun and Paragraph to WordDocument Object 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. 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 Open Visual Studio. Create a new C# Console .NET Application. Name your project and choose a location. From Additional Information, select the latest version of the .NET Framework. IronWord supports the latest 8.0 .NET Core Framework. 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 Right-click on your project in Solution Explorer. Select "Manage NuGet Packages for Solution." Search for "IronWord" in the NuGet Package Manager browse tab. 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. 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 库,创建TextRun和Paragraph对象,然后使用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 软件提供的官方 IronWord 文档页面上找到。 程序化生成 Word 文档为何重要? 程序化生成 Word 文档对于自动化文档创建和操作非常重要,这提高了各种应用程序和行业的生产力和效率。 在 Visual Studio 中设置一个 Word 文档项目的基本步骤是什么? 基本步骤包括创建一个 Visual Basic 项目,通过 NuGet 包管理器安装 IronWord,并将TextRun和Paragraph对象集成到WordDocument中以导出为 DOCX 文件。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已更新九月 18, 2025 ASP .NET Core 导入和导出 Word 文件 本指南探讨了如何使用 IronWord 库导入现有的 Word 文档、显示其内容并从头创建文档 阅读更多 已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多 已更新六月 22, 2025 如何使用 C# 在 Word 中对齐文本 深入了解 IronWord NuGet 包以及使用该包对齐文本或段落的方法 阅读更多 如何在 C# 中创建 DOCX 文件如何在 C# 中不需要 Office In...
已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多