使用IRONWORD

C#编辑Word(代码示例开发教程)

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

创建、编辑和管理Word 文档是许多应用程序的常见需求。 虽然有多种方法可以用 C# 创建和编辑 Word 文档,但最强大的方法之一是使用 Microsoft Interop 服务。 有了这个工具,您可以轻松地以编程方式处理 Word 文档。

先决条件

在设置环境和开始编写代码之前,请确保您满足以下前提条件:

  1. Visual Studio:确保您的计算机上已安装Visual Studio。如果没有,请从微软官方网站下载并安装。

  2. Microsoft Word:由于我们使用 Microsoft Interop,您的计算机上应安装MS Word。 Interop 服务可与您机器上安装的 Microsoft Word 应用程序连接。

  3. 基本的C#知识 理解基本的C#

  4. .NET Framework:确保您的 Visual Studio 支持 .NET Framework,因为我们的应用程序将基于此。

设置环境

首先打开 Visual Studio 应用程序。 打开后,您将看到一个欢迎屏幕。

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

  1. 单击 "创建新项目"。

  2. 在搜索框中输入“Console App (.NET Framework)”。

  3. 从结果中选择“控制台应用 (.NET Framework)”,然后点击“下一步”按钮。

  4. 为您的项目设置一个名称,然后单击 "创建 "按钮。

    完成这些步骤后,Visual Studio 将为您生成一个新的 .NET Framework 控制台应用程序。 在Program.cs文件中,您会找到一个带有Main方法的基本模板,它是控制台应用程序的入口点。

2. 使用 NuGet 包管理器安装 Microsoft.Office.Interop.Word

NuGet 是 .NET 的软件包管理器,已集成到 Visual Studio 中。 以下是安装Microsoft.Office.Interop.Word包的方法:

  1. 在 Visual Studio 中,进入 "工具 "菜单。

  2. 选择 "NuGet 包管理器",然后选择 "管理解决方案的 NuGet 包..."。

  3. 在 NuGet 窗口中,点击 "浏览 "选项卡。

  4. 在搜索框中,输入Microsoft.Office.Interop.Word并按回车。

  5. 从搜索结果中选择Microsoft.Office.Interop.Word包。

  6. 在右侧,确保选中您的控制台应用程序项目,然后点击 "安装 "按钮。

    C# 编辑 Word(代码示例开发者教程)图 1

    Visual Studio 现在将安装该软件包,并在项目中添加对它的引用。 此软件包包含与您的 C# 应用程序交互的必要程序集和工具,用于操作 MS Word

介绍 IronWord:Interop 的卓越替代品

虽然 Interop 提供了使用 Word 和 Excel 的强大功能,但它也有局限性。 IronWord 是一个专为 .NET 开发人员优化的多功能库。 IronWord 提供了比 Interop 更流畅的体验,尤其是在编辑 Word 文档任务方面。 它不仅要确保兼容性和性能,还要用直观的方法简化复杂的任务。 为了便于比较,在MS Word之后,我将为每个用例提供IronWord代码片段,使用IronWord版本2024.1.2

打开现有 Word 文档

通常情况下,您可能需要编辑现有的 Word 文档,下面的示例展示了如何用 C# 来完成这项工作:

var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
Dim WordDoc = WordApp.Documents.Open("path_to_your_document.docx")
$vbLabelText   $csharpLabel

在上述代码中,将path_to_your_document.docx替换为docx文件的路径。

使用IronWord

使用 IronWord 打开 Word 文档。

WordDocument doc = new WordDocument(@"path_to_your_document.docx");
WordDocument doc = new WordDocument(@"path_to_your_document.docx");
Dim doc As New WordDocument("path_to_your_document.docx")
$vbLabelText   $csharpLabel

创建新的 Word 文档

从零开始创建 Word 文档:

var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Add();
var WordApp = new Microsoft.Office.Interop.Word.Application();
var WordDoc = WordApp.Documents.Add();
Dim WordApp = New Microsoft.Office.Interop.Word.Application()
Dim WordDoc = WordApp.Documents.Add()
$vbLabelText   $csharpLabel

该代码片段将创建一个新的 Word 文档,您可以使用 C# 进行编写和编辑。

使用IronWord

WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
$vbLabelText   $csharpLabel

使用 C#

在 Word 文档中添加文本

添加新的一段文字:

WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";
WordDoc.Paragraphs.Add()
WordDoc.Paragraphs (1).Range.Text = "This is the first paragraph."
$vbLabelText   $csharpLabel

Paragraphs.Add() 方法向 Word 文档添加一个新段落,而 Range.Text 属性为其分配新文本。

使用IronWord

doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
$vbLabelText   $csharpLabel

编辑现有文本

在本教程中,我们来更改第一段

WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";
WordDoc.Paragraphs (1).Range.Text = "This is the edited first paragraph."
$vbLabelText   $csharpLabel

您还可以使用类似的方法在 Word 文档中添加和编辑其他元素。

使用IronWord

doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";
doc.Paragraphs (0).TextRuns (0).Text = "This is the edited first paragraph."
$vbLabelText   $csharpLabel

保存和关闭文档

一旦您完成了所需的编辑:

WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
WordDoc.SaveAs("path_where_you_want_to_save.docx")
WordDoc.Close()
WordApp.Quit()
$vbLabelText   $csharpLabel

path_where_you_want_to_save.docx 替换为您想要的路径。

使用IronWord

doc.SaveAs(@"path_where_you_want_to_save.docx");
doc.SaveAs(@"path_where_you_want_to_save.docx");
doc.SaveAs("path_where_you_want_to_save.docx")
$vbLabelText   $csharpLabel

完整代码和示例

让我们一起来看看吧。 下面是一个完整的代码示例,演示了如何打开现有 Word 文档、编辑文档并保存更改:

var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs [1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs [1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();
Dim WordApp = New Microsoft.Office.Interop.Word.Application()

' Create a new Word document
Dim WordDoc = WordApp.Documents.Add()

' Add new text
WordDoc.Paragraphs.Add()
WordDoc.Paragraphs (1).Range.Text = "This is the first paragraph."

' Edit the first paragraph
WordDoc.Paragraphs (1).Range.Text = "This is the edited first paragraph."

' Save and close
WordDoc.SaveAs("path_where_you_want_to_save.docx")
WordDoc.Close()
WordApp.Quit()
$vbLabelText   $csharpLabel

使用IronWord

与 MS Word 相比的完整代码示例。 IronWord 利用简洁的代码片段编辑 DOCX 文件。

// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit text
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";

// Export docx
doc.SaveAs(@"path_where_you_want_to_save.docx");
// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit text
doc.Paragraphs [0].TextRuns [0].Text = "This is the edited first paragraph.";

// Export docx
doc.SaveAs(@"path_where_you_want_to_save.docx");
' Create an empty Word document
Dim doc As New WordDocument()

' Add new text
doc.AddText("This is the first paragraph.")

' Edit text
doc.Paragraphs (0).TextRuns (0).Text = "This is the edited first paragraph."

' Export docx
doc.SaveAs("path_where_you_want_to_save.docx")
$vbLabelText   $csharpLabel

结论

在.NET 应用程序中处理 Word 和 Excel 文档方面,有很多选择。 虽然微软的 Interop 服务一直是许多人的首选,但 IronWord 等解决方案的出现标志着向更高效和用户友好型工具的转变。

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

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

查看许可证 >