使用IRONWORD

C# 打开Word文档

雷根·彭
雷根·彭
2023年十一月13日
分享:

Word 应用文档在专业和个人交流的各个方面都不可或缺。 对于希望将任务自动化或将文档处理集成到应用程序中的开发人员来说,以编程方式操作 Microsoft Word 文档文件并与之交互的能力至关重要。 为了能够用 C# 编程处理 Microsoft Word 文档,有许多文档库可用。 其中一个这样的库是IronWord,这是由Iron Software开发的一个强大的C# Word DOCX库,简化了在.NET应用程序中处理Word文档的过程。

在本文中,我们将探讨强大的IronWord - The C# Library、其功能、打开Word文档集合以及从中读取数据。

如何在C#中打开Word文档集

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

  2. 安装 IronWord C# DOCX 库

  3. 使用WordDocument类打开Word文档

  4. 使用Paragraph类遍历每个段落

  5. 在每个段落上运行TextRuns

  6. 显示内容或使用SaveAs方法进行保存

IronWord - C# DOCX 库

IronWord 是一个功能丰富的 C# Word DOCX 库,由 Iron Software 开发。 它提供了一个用户友好型 API,使开发人员能够在其 .NET 应用程序中轻松处理 Word 文档。 无论您是创建新的 Word 文档、编辑现有文档还是提取内容,IronWord 都能提供一套全面的工具来简化流程。

功能设置

1.兼容性和跨平台支持

IronWord 的设计用途广泛,支持各种 .NET 版本,包括 .NET 8、7、6、Framework、Core 和 Azure。 开发人员可以在 Windows、Linux、macOS、iOS、Android 等不同平台上使用它,使其能够适应广泛的 .NET 应用程序开发场景。

2.文档处理

IronWord 的功能不仅限于简单的文档创建。 它允许复杂的文档操作,包括文本和段落格式化、图像和形状集成、表格创建等。 这种多功能性使 IronWord 适用于对文档结构和内容进行精确控制至关重要的各种应用。

3.不依赖于 Microsoft Office

IronWord 的一个显著特点是它独立于微软 Office 安装或 Word Interop。这意味着无需安装 Word 应用程序。 开发人员可以利用其功能,而不必担心额外的依赖性,从而确保开发过程更加顺畅、高效。

4.易用性

该库采用用户友好型 API,允许开发人员将 Word 文档处理功能无缝集成到他们的 .NET 项目中。 此外,IronWord 无需安装 Microsoft Office 或 Word Interop,确保了无忧的开发体验。

先决条件

在进入 IronWord 的世界之前,请确保您已具备以下先决条件:

  • Visual Studio:确保您已安装正常运行的Visual Studio,这是用于.NET开发的流行集成开发环境。 您可以从这里下载。
  • IronWord: 您需要下载该库以使用其功能。 您可以直接从这里下载NuGet包。

设置环境

首先,打开Visual Studio,您将看到欢迎屏幕。

1.创建新的 .NET Framework 控制台应用程序

点击“创建新项目”。搜索“控制台应用程序 (.NET Framework)”,从列表中选择它,然后点击“下一步”。命名你的项目并点击“创建”。Visual Studio 将用一个基本模板设置一个新的 .NET Framework 控制台应用程序,包括一个作为入口点的 Main 方法。

新项目配置

2. 使用 NuGet 包管理器安装 IronWord

在 Visual Studio 中,导航到“工具”菜单,选择“NuGet 包管理器”,然后选择“管理解决方案的 NuGet 包”。在 NuGet 窗口中,转到“浏览”选项卡,在搜索框中输入“IronWord”,然后按回车键。 从结果中选择包,确保在右侧选中您的控制台应用程序项目,然后点击“安装”。这将为在您的C#应用程序中使用IronWord添加必要的引用。 现在您可以开始使用IronWord来处理Word文档了。

IronWord

3. 在代码中添加对 IronWord 的引用:

在您的 C# 代码文件中,在 Program.cs 文件中添加以下 using 语句以引用 IronWord:

using IronWord;
using IronWord;
Imports IronWord
$vbLabelText   $csharpLabel

打开 Word 文档并阅读内容的步骤

现在我们的项目已经建立,请按照以下步骤使用 IronWord 打开一个 Word 文档并阅读其中的内容:

  1. 加载现有文档:
// Load existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
// Load existing Word document file
WordDocument doc = new WordDocument("existing_document.docx");
' Load existing Word document file
Dim doc As New WordDocument("existing_document.docx")
$vbLabelText   $csharpLabel

在这一步中,我们从IronWord库中创建WordDocument类的一个实例。 我们使用构造函数,其采用指向现有输入Word文档(existing_document.docx)的路径。 这会初始化doc对象发送者,表示从输入文件名称加载的Word文档。

输入文件:

输入

  1. 阅读和操作内容:

    以下代码有助于从打开的文档文件中读取文本内容:

// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (TextRun textRun in paragraph.TextRuns)
    {
        // Access text content
        string content = textRun.Text;
    // Display contents
        Console.WriteLine(content);
    }
}
// Access paragraphs and text runs
foreach (Paragraph paragraph in doc.Paragraphs)
{
    foreach (TextRun textRun in paragraph.TextRuns)
    {
        // Access text content
        string content = textRun.Text;
    // Display contents
        Console.WriteLine(content);
    }
}
' Access paragraphs and text runs
For Each paragraph As Paragraph In doc.Paragraphs
	For Each textRun As TextRun In paragraph.TextRuns
		' Access text content
		Dim content As String = textRun.Text
	' Display contents
		Console.WriteLine(content)
	Next textRun
Next paragraph
$vbLabelText   $csharpLabel

在这里,我们遍历加载的 Word 文档(doc)中的段落和文本运行。 foreach 循环允许我们遍历每个段落,并在其中嵌套每个文本运行。 对于每个textRun,我们可以使用textRun.Text访问其文本内容。 在这一点上,您可以执行任何所需的操作,例如以编程方式提取信息或修改文本内容。

  1. 显示内容和输出:
// Display contents
Console.WriteLine(content);
// Display contents
Console.WriteLine(content);
' Display contents
Console.WriteLine(content)
$vbLabelText   $csharpLabel

在上一步的第二个 foreach 循环中,我们将在控制台输出屏幕上显示单词的可见内容。 我们还可以将已打开文档的部分内容保存为新文档:

// Method to save changes to the document
doc.SaveAs("modified_document.docx");
// Method to save changes to the document
doc.SaveAs("modified_document.docx");
' Method to save changes to the document
doc.SaveAs("modified_document.docx")
$vbLabelText   $csharpLabel

完整的程序代码如下

using IronWord;
using IronWord.Models;

namespace IronWordExample
{
    class Program
    {
        public static void main(string [] args)
        {
            // Load existing Word doc file
            WordDocument doc = new WordDocument("existing_document.docx");

            // Access paragraphs and text runs
            foreach (Paragraph paragraph in doc.Paragraphs)
            {
                foreach (TextRun textRun in paragraph.TextRuns)
                {
                    // Access text content
                    string content = textRun.Text;
                    // Display Contents
                    Console.WriteLine(content);
                }
            }

            // Save changes to the document
            doc.SaveAs("modified_document.docx");
        }
    }
}
using IronWord;
using IronWord.Models;

namespace IronWordExample
{
    class Program
    {
        public static void main(string [] args)
        {
            // Load existing Word doc file
            WordDocument doc = new WordDocument("existing_document.docx");

            // Access paragraphs and text runs
            foreach (Paragraph paragraph in doc.Paragraphs)
            {
                foreach (TextRun textRun in paragraph.TextRuns)
                {
                    // Access text content
                    string content = textRun.Text;
                    // Display Contents
                    Console.WriteLine(content);
                }
            }

            // Save changes to the document
            doc.SaveAs("modified_document.docx");
        }
    }
}
Imports IronWord
Imports IronWord.Models

Namespace IronWordExample
	Friend Class Program
		Public Shared Sub main(ByVal args() As String)
			' Load existing Word doc file
			Dim doc As New WordDocument("existing_document.docx")

			' Access paragraphs and text runs
			For Each paragraph As Paragraph In doc.Paragraphs
				For Each textRun As TextRun In paragraph.TextRuns
					' Access text content
					Dim content As String = textRun.Text
					' Display Contents
					Console.WriteLine(content)
				Next textRun
			Next paragraph

			' Save changes to the document
			doc.SaveAs("modified_document.docx")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

输出 要了解 IronWord 可以执行的更多功能,请访问此代码示例页面。

结论

在本文中,我们探讨了 IronWord 的功能,这是一个强大的 C# Word DOCX 库,可以简化以编程方式打开和操作 Word 文档的过程。 通过提供丰富的功能集和消除对外部软件的依赖,IronWord 使开发人员能够将文档处理无缝集成到他们的 .NET 应用程序中。 无论您是要实现文档相关任务的自动化,还是要增强体验,事实证明 IronWord 都是您 .NET 工具包中的宝贵工具。

要了解更多信息并开始将IronWord集成到您的新应用程序项目中,请访问文档页面

IronWord提供免费试用来测试其完整功能。 这有助于您在购买前做出明智的决定。 其 Lite 许可证的起始价格为$749,更多详细信息可以在此许可证页面找到。

点击这里免费试用IronWord。

雷根·彭
软件工程师
Regan毕业于雷丁大学,拥有电子工程学士学位。在加入Iron Software之前,他的前工作职位要求他专注于单一任务;他在Iron Software最喜欢的是能进行多种工作,无论是增加销售价值、技术支持、产品开发还是营销。他喜欢了解开发人员如何使用Iron Software的库,并利用这些知识不断改进文档和开发产品。
< 前一页
如何在C#中将Word转换为PDF
下一步 >
C# 打印 Word 教程:分步指南

准备开始了吗? 版本: 2025.4 刚刚发布

查看许可证 >