使用IRONWORD

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

发布 2023年十一月14日
分享:

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

先决条件

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

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

  2. 微软 Word:由于我们使用的是 Microsoft Interop,因此您应该有 MS Word 安装在您的计算机上。Interop 服务与计算机上安装的 Microsoft Word 应用程序连接。

  3. Basic C# Knowledge 了解基本的 C#

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

设置环境

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

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

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

2.键入 "控制台应用程序 (.NET框架)在搜索框中输入"......"。

3.从搜索结果中选择 "控制台应用程序 (.NET框架)"然后点击 "下一步 "按钮。

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 为 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")
VB   C#

在上述代码中,将 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")
VB   C#

创建新的 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()
VB   C#

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

使用IronWord

WordDocument doc = new WordDocument();
WordDocument doc = new WordDocument();
Dim doc As New WordDocument()
VB   C#

使用 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."
VB   C#

段落添加()方法在 Word 文档中添加了一个新段落,Range.Text 属性为其分配了新文本。

使用IronWord

doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord");
doc.AddText("Add text using IronWord")
VB   C#

编辑现有文本

在本教程中,让我们修改第一段

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."
VB   C#

您还可以使用类似的方法在 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."
VB   C#

保存和关闭文档

完成所需的编辑后:

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()
VB   C#

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")
VB   C#

完整代码和示例

让我们一起来看看。下面是一个完整的代码示例,演示如何打开现有 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()
VB   C#

使用 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")
VB   C#

结论

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

< 前一页
如何在C#中创建Word文档
下一步 >
如何在C#中将Word转换为PDF

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

免费NuGet下载 总下载量: 4,816 查看许可证 >