跳至页脚内容
使用 IRONWORD

如何使用 C# 读取 Word 文件

在当今时代,Microsoft Word 文档已成为办公工作以及专业和个人沟通的代名词。 因此,对于希望在应用程序中自动执行向用户显示的任务的开发人员来说,以编程方式操作 Word 文档至关重要。 虽然有很多可用的库,但并非所有库都像其他库一样强大。 然而, IronWord就是众多竞争者中脱颖而出的一个。 IronWord是一个可靠且强大的 C# Word DOCX 库,易于使用和理解,简化了 Word 文档的处理。

本文将通过简短的示例,探讨如何快速利用IronWord阅读 Word 文档。

如何使用 C# 读取 Word 文件

  1. 在 Visual Studio 中创建一个控制台应用程序

  2. 安装 IronWord C# DOCX 库

  3. 使用WordDocument类创建一个新的 Word 文档

  4. 向 Word 文档添加文本

  5. 使用Paragraph类遍历每个段落。

  6. 显示内容

IronWord:C# DOCX 库

IronWord:C# DOCX 库

IronWord是一个灵活且可扩展的库,它具有简单易用的 API,可以消除将 Word 文档集成到应用程序中的麻烦。 无论您是想在应用程序中添加和集成简单的文本 Word 文档,还是创建复杂的表格和报告以显示给用户,IronWord 都能满足您的所有需求。

它最显著的特点如下:

1.文档操作:IronWord 允许开发人员轻松操作 Word 文档。 无论用户想要插入文本段落还是更复杂的结构,例如表格和图像,IronWord 都能做到。

2.跨平台支持和兼容性: IronWord 的设计具有灵活性,支持多个平台上的开发人员。 它支持各种 .NET Core(8、7、6、5 和 3.1+)、.NET Standard(2.0+)、.NET Framework(4.6.2+),甚至 Azure。 此外,开发者可以在不同的平台和系统上使用 IronWord,包括但不限于 Windows、Linux、macOS 和 Android。 它涵盖了最常见的平台,并允许开发人员快速构建跨平台应用程序。

3.独立于 Microsoft Office:在 .NET 应用程序中集成 Word 文档时,一个常见的问题是,像 Interop 这样的常用库需要安装 Microsoft Office 的许可才能运行。然而,IronWord 通过独立于此限制解决了这个问题。 开发者可以充分利用 Word 文档的强大功能,而不受 Microsoft Word 的许可和安装限制。

4.格式选项:IronWord 提供广泛的格式和样式支持,使开发人员能够使文档独具特色。 开发人员可以为文本应用字体、颜色、对齐方式和其他复杂格式,例如表格样式

5.易用性和广泛的支持:除了易于理解的 API 和直接的方法调用外,IronWord 还提供扩展 API 参考和代码示例,以帮助开发人员确定使用 IronWord 的最佳方式。

在 Visual Studio 中创建新的控制台项目

在深入示例之前,让我们先在Visual Studio中创建一个空白的控制台项目。

创建新项目时,请点击控制台应用程序

然后我们为项目提供名称和保存位置。

! 指定项目的名称和位置路径。

接下来,选择您将要使用的 .NET 框架。 在这个例子中,我将使用.NET 8.0。

选择所需的 .NET 框架,然后单击"创建"。

创建并设置好新的控制台项目后,让我们安装我们的 C# 单词库IronWord

安装 IronWord

安装 IronWord 有两种方法。

1. 通过 NuGet 包管理器安装

要通过 NuGet 程序包管理器安装它,请单击"工具",然后Manage NuGet Packages for Solution 。 然后我们在搜索栏中搜索 IronWord 并安装**IronWord**

! 使用 NuGet 包管理器管理解决方案中的 NuGet 包来安装 IronWord,方法是在 NuGet 包管理器的搜索栏中搜索"IronWord",然后选择项目并单击"安装"按钮。

2. 通过 NuGet 包管理器控制台安装

另一种方法是通过 NuGet 程序包管理器控制台进行安装。 为此,请在控制台中运行以下命令:

Install-Package IronWord

许可证密钥

请注意,没有许可证密钥,IronWord 无法运行; 您可以在这里免费试用一次。

获取试用密钥后,请确保在您的项目中设置此变量。

// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
// Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";
' Replace the license key variable with the trial key you obtained
IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"
$vbLabelText   $csharpLabel

在 C# 中读取 Word 文档

安装好必备组件后,我们将通过以下代码演示使用IronWord读取 Microsoft Word 文档的简易性。

首先,我们导入必要的命名空间。 使用 IronWord,我们创建一个新文档并添加示例文本。 然后我们使用WordDocument对象访问段落和文本,以打印 Word 文档中的文本。

using IronWord;
using IronWord.Models;

#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion

// Create text run with sample text
Text textRunExample = new Text("Sample text");

// Create a paragraph and add the text run to it
Paragraph paragraphExample = new Paragraph();
paragraphExample.AddChild(textRunExample);

// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraphExample);

// Export the document as a DOCX file
doc.SaveAs("document.docx");

// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (Text textRun in paragraph.Texts)
    {
        // Access text content
        string content = textRun.Text;
        // Display the content to the console
        Console.WriteLine(content);
    }
}
using IronWord;
using IronWord.Models;

#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion

// Create text run with sample text
Text textRunExample = new Text("Sample text");

// Create a paragraph and add the text run to it
Paragraph paragraphExample = new Paragraph();
paragraphExample.AddChild(textRunExample);

// Create a new Word document with the paragraph
WordDocument doc = new WordDocument(paragraphExample);

// Export the document as a DOCX file
doc.SaveAs("document.docx");

// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (Text textRun in paragraph.Texts)
    {
        // Access text content
        string content = textRun.Text;
        // Display the content to the console
        Console.WriteLine(content);
    }
}
Imports IronWord
Imports IronWord.Models

#Region "Licensing"
' Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE"
'#End Region

' Create text run with sample text
Dim textRunExample As New Text("Sample text")

' Create a paragraph and add the text run to it
Dim paragraphExample As New Paragraph()
paragraphExample.AddChild(textRunExample)

' Create a new Word document with the paragraph
Dim doc As New WordDocument(paragraphExample)

' Export the document as a DOCX file
doc.SaveAs("document.docx")

' Access paragraphs and text runs within the document
For Each paragraph As Paragraph In doc.Paragraphs
	For Each textRun As Text In paragraph.Texts
		' Access text content
		Dim content As String = textRun.Text
		' Display the content to the console
		Console.WriteLine(content)
	Next textRun
Next paragraph
$vbLabelText   $csharpLabel

让我们来探讨一下从上面的代码中读取 word 文件的方法和参数。

  1. 我们首先创建一个Text对象,并将字符串"示例文本"赋值给它。

  2. 然后我们实例化一个Paragraph对象,并将"textRunExample"添加到该对象中。

  3. 我们还实例化了一个WordDocument对象,将其命名为WordDocument doc,并将paragraphExample传递给它,以创建一个包含该段落的新 Word 文档。

  4. 该代码将 Word 文档保存为文件名"document.docx",以便稍后使用。

  5. 要访问我们刚刚创建的 Word 文档中的段落,我们可以访问WordDocument对象的"Paragraphs"属性。 "Paragraphs"属性是一个列表。因此,我们需要使用foreach循环来遍历它。

  6. 要获取段落中的文本,我们可以访问Paragraphs的"Texts"属性。 这也会返回一个文本列表。

  7. 最后,我们将文本赋值给名为"content"的字符串变量,并将其打印到控制台。

控制台输出

! 控制台输出显示从 Word 文档 doc 读取的文本。

在 C# 中读取现有的 Word 文档

在前面的例子中,我们通过编程方式创建了一个新的 Word 文档并读取了它的内容。 我们可以通过对代码进行一些修改,按照类似的步骤读取现有的 Word 文档。

输入文档

输入示例:长篇 Word 文档

using IronWord;
using IronWord.Models;

#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion

// Load an existing Word document
WordDocument doc = new WordDocument("existing_document.docx");

// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (Text textRun in paragraph.Texts)
    {
        // Access text content
        string content = textRun.Text;
        // Display the content to the console
        Console.WriteLine(content);
    }
}
using IronWord;
using IronWord.Models;

#region Licensing
// Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE";
#endregion

// Load an existing Word document
WordDocument doc = new WordDocument("existing_document.docx");

// Access paragraphs and text runs within the document
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (Text textRun in paragraph.Texts)
    {
        // Access text content
        string content = textRun.Text;
        // Display the content to the console
        Console.WriteLine(content);
    }
}
Imports IronWord
Imports IronWord.Models

#Region "Licensing"
' Set the IronWord license key before using the IronWord functionalities
IronWord.License.LicenseKey = "YOUR-KEY-HERE"
'#End Region

' Load an existing Word document
Dim doc As New WordDocument("existing_document.docx")

' Access paragraphs and text runs within the document
For Each paragraph As Paragraph In doc.Paragraphs
	For Each textRun As Text In paragraph.Texts
		' Access text content
		Dim content As String = textRun.Text
		' Display the content to the console
		Console.WriteLine(content)
	Next textRun
Next paragraph
$vbLabelText   $csharpLabel

这两个例子的主要区别在于传递给WordDocument对象的参数。 我们不创建新文档,而是将现有的 Word 文件加载到其中。 其余部分与另一个例子相同。

控制台输出

控制台输出

结论

IronWord 许可信息

在这些示例中,我们演示了使用IronWord库以编程方式在 C# 中操作和读取 Word 文档是多么简单。 IronWord库的灵活性和可扩展性使其成为一个有价值的工具,使开发人员能够在实际的、真实的示例中使用 IronWord,例如填写模板、生成报告和批量处理文档。 了解 Word 如何与应用程序集成非常重要,因为它能为开发人员提供更多解决问题的方案。

此外,开发者可以在购买前试用IronWord 的丰富功能一段时间,因为它提供免费试用许可证。 除了易于使用之外,IronWord 还附带详尽的文档和 24/5 全天候支持,从而减轻开发人员在生产过程中可能遇到的持续挫折感。 我们还提供各种教程和一系列代码示例供您参考,以帮助您开始使用IronWord

在使用试用许可证测试IronWord的各个方面之后,您可以购买我们的 Lite 开发人员许可证,起价为 599 美元,价格从 Lite 到 Professional 依次递增。 更多信息请参阅我们的许可页面。

常见问题解答

如何使用C#读取Word文档?

您可以使用IronWord在C#中读取Word文档。只需使用WordDocument类加载文档,然后遍历段落和文本运行来访问和显示文本内容。

什么是可靠的C#库用于读取Word文档?

IronWord是专为读取和操作Word文档而设计的可靠C#库。它提供了一个简单的API,使应用程序中的Word文档功能集成变得简单。

在C#中读取Word文档需要安装Microsoft Office吗?

不,IronWord不需要安装Microsoft Office。它独立运行,可以在不需要Office许可版本的情况下操作Word文档。

如何在Visual Studio中安装用于读取Word文档的C#库?

您可以通过Visual Studio中的NuGet包管理器搜索'IronWord'并选择'安装',或者通过NuGet包管理器控制台使用Install-Package IronWord命令来安装IronWord。

IronWord支持哪些平台进行Word文档操作?

IronWord支持包括.NET Core(8, 7, 6, 5, 和 3.1+)、.NET Standard(2.0+)、.NET Framework(4.6.2+)和Azure在内的各种平台,并与Windows、Linux、macOS和Android兼容。

我可以使用C#库操作现有的Word文档吗?

可以,IronWord允许您以编程方式读取和修改新旧Word文档,提供对文档内容和结构的全面控制。

IronWord是否提供免费试用?

是的,IronWord提供免费试用许可证。开发者可以通过试用探索其功能,并在购买完整许可证之前评估其在项目中的适用性。

如何使用C#将Word文档转换为其他格式?

您可以利用IronWord的API将Word文档导出为PDF、HTML等格式,尽管具体转换可能有所不同。

IronWord在C#应用程序中的一些常见用例是什么?

IronWord常用于完成模板、生成报告、批量处理文档以及将Word文档功能集成到.NET应用程序中。

IronWord用户有哪些支持选项可用?

IronWord提供24/5支持,并附有全面的文档、教程和代码示例,帮助开发者有效使用该库。

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