跳至页脚内容
使用 IRONWORD

如何在 C# 中将文档导出为 Word

Microsoft Word is a ubiquitous tool for creating and editing documents, widely used across various industries for its versatile features and user-friendly interface. In software development, there are often scenarios where we need to programmatically generate Word documents, either for reporting, documentation, or data presentation purposes.

In this guide, we'll explore how to create a new C# Console Application and how to export data to MS Word documents in C# using IronWord, a powerful library for manipulating Word documents programmatically.

How to Export Document to Word in C#

  1. Create a New C# Visual Studio Project.
  2. Install the C# Word library to export data to Word.
  3. Import dependencies for the Word Library.
  4. Create a new Word Document using the "new WordDocument(paragraph)" method.
  5. Save the newly created Word document with the "SaveAs()" function.

Create a New C# Console Application

To create a new C# Console application in Visual Studio, follow these steps.

  1. Open Visual Studio. On the startup screen, select the "Create a new project" option.

How to Export Document to Word in C#: Figure 1 - Visual Studio

  1. A new window will appear containing the recent templates and all the templates available. Select the "Console App" option and click on Next.

How to Export Document to Word in C#: Figure 2 - Console App

  1. In the newly appeared window, write the project name and select the location. Click on Next, and then after selecting the version, click on Create.

IronWord

IronWord is a .NET library that provides a convenient API for working with Word documents in C#. It allows developers to create Word documents, modify existing Word documents, and export Word documents seamlessly within their C# applications. With IronWord, you can generate Word documents dynamically based on data from various sources, such as databases, APIs, or user inputs.

IronWord fosters creativity and expression, providing a space where emerging voices mingle with seasoned wordsmiths. Through its commitment to literary excellence and diversity, IronWord cultivates a rich tapestry of narratives that resonate with audiences worldwide, igniting imaginations and sparking conversations that endure beyond the page.

Install IronWord

You can easily install IronWord using the NuGet Package Manager by following these steps.

  1. In Visual Studio, open the NuGet Package Manager window and go to the Browse tab.
  2. In the Browse tab, write IronWord in the search bar and press enter.
  3. A list of packages will appear. Select the latest package and click on Install.

How to Export Document to Word in C#: Figure 3 - IronWord

Just like that, IronWord is installed and ready to use.

Export Data to a Word Document

Let's dive into a practical example of how to export data to a Word document using IronWord in C#. Consider the following code snippet:

using IronWord;
using IronWord.Models;

System.Console.WriteLine("Enter the Text to export it to word document");
var userInput = System.Console.ReadLine();

// Create textRun which is a part of a paragraph content
Text textRun = new Text(userInput);
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);

// Create a new document object and add the paragraph to it
WordDocument doc = new WordDocument(paragraph);

// Export document to "document.docx" file
doc.SaveAs("document.docx");
using IronWord;
using IronWord.Models;

System.Console.WriteLine("Enter the Text to export it to word document");
var userInput = System.Console.ReadLine();

// Create textRun which is a part of a paragraph content
Text textRun = new Text(userInput);
Paragraph paragraph = new Paragraph();
paragraph.AddChild(textRun);

// Create a new document object and add the paragraph to it
WordDocument doc = new WordDocument(paragraph);

// Export document to "document.docx" file
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models

System.Console.WriteLine("Enter the Text to export it to word document")
Dim userInput = System.Console.ReadLine()

' Create textRun which is a part of a paragraph content
Dim textRun As New Text(userInput)
Dim paragraph As New Paragraph()
paragraph.AddChild(textRun)

' Create a new document object and add the paragraph to it
Dim doc As New WordDocument(paragraph)

' Export document to "document.docx" file
doc.SaveAs("document.docx")
$vbLabelText   $csharpLabel

In this example, we're creating a simple Word document containing a single paragraph with the text from the user. Let's break down the code step by step:

  1. Importing Required Libraries: We begin by importing the necessary namespaces from IronWord.
  2. Creating Text Content: We create a Text object representing the text content we want to include in the Word document. In this case, it's the userInput.
  3. Creating a Paragraph: Next, we create a Paragraph object and add the Text object (textRun) to it as a child element. A paragraph in Word typically contains one or more text runs.
  4. Creating a Word Document: We instantiate a new WordDocument object, passing the paragraph we created as a parameter. This initializes a new Word document with the specified content.
  5. Exporting the Document: Finally, we call the SaveAs method on the WordDocument object to export the document to a .docx file named "document.docx."

This example demonstrates the basic workflow for exporting data to a Word document using IronWord. However, it's important to note that you can customize the existing Word document content and structure according to your specific requirements. For instance, you can add multiple paragraphs, format text, insert tables, images, headers, footers, and more.

Console Screenshot

How to Export Document to Word in C#: Figure 4 - Export Data Console Output

Output Word Document

How to Export Document to Word in C#: Figure 5 - Create Word Document Output

Conclusion

In summary, IronWord provides a powerful and flexible solution for exporting data to Word documents in C#. Whether you're generating simple reports, complex documents, or dynamic content, IronWord simplifies the process and empowers developers to create high-quality Word documents programmatically. By leveraging its features and capabilities, you can streamline document generation workflows and deliver compelling content to your users.

To learn more techniques to automate Microsoft Word document generation, visit the following link.

常见问题解答

我如何在 C# 中输出数据到 Word 文档?

您可以使用 IronWord 在 C# 中输出数据到 Word 文档,通过创建 C# 控制台应用程序,使用 NuGet 包管理器安装 IronWord,导入必要的库,并使用 API 创建和保存包含数据的 Word 文档。

使用 .NET 库创建 Word 文档有哪些好处?

使用诸如 IronWord 的 .NET 库来创建 Word 文档可以让开发人员以编程方式自动生成高质量文档,从而提高工作流程的效率,并适应特定需求而无需依赖 Microsoft Interop。

如何在 C# 中格式化文本和添加图片到 Word 文档?

通过 IronWord,您可以通过设置字体样式、大小和颜色来格式化文本,并通过使用 API 将图片插入到文档中来添加图片。这使 C# 应用程序中的文档定制更丰富。

IronWord 可以用于动态文档生成吗?

是的,IronWord 允许根据各种来源的数据(如数据库或用户输入)创建 Word 文档,从而可以定制以满足特定用户需求。

使用 IronWord 创建 Word 文档有哪些步骤?

要使用 IronWord 创建 Word 文档,首先要设置一个 C# 控制台应用程序,使用 NuGet 安装 IronWord,导入库,创建文本和段落对象,并使用 SaveAs 方法导出文档。

IronWord 如何简化 C# 中 Word 文档的创建?

IronWord 简化 Word 文档创建过程,通过提供一个简单直观的 API,让开发人员可以轻松以编程方式创建、操作和导出 Word 文档,从而减少复杂性并提高生产力。

能否在 C# 中根据用户输入创建 Word 文档?

是的,使用 IronWord,您可以通过捕获用户输入、创建文本对象、将其添加到段落中,然后使用 IronWord API 将其导出为 .docx 文件来创建 Word 文档。

在 C# 中输出到 Word 时如何解决常见问题?

在使用 IronWord 输出到 Word 时进行故障排除时,确保导入了所有必要的库,通过 NuGet 安装的是正确版本,并确保您的数据类型与所使用的 API 方法兼容。

程序生成 Word 文档的实际应用有哪些?

使用 IronWord 程序生成 Word 文档对于创建自动化报告、文档和数据展示文档非常有用,尤其是在处理大型数据集或频繁更新的信息时。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。