C# 操作 Word 全域查找且替换(含图片对象)

2024-03-29 20:20

本文主要是介绍C# 操作 Word 全域查找且替换(含图片对象),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

关于全域查找且替换

Word应用样本

SqlServer数据表部分设计样本

范例运行环境

配置Office DCOM

设计实现

组件库引入

实现原理

查找且替换的核心代码

窗格内容

页眉内容

页脚内容

形状内容

小结


关于全域查找且替换

C#全域操作 Word 查找且替换主要包括如下四个对象:

序号对象说明
1Word.Appication.Selection窗格对象
2Word.Section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range页眉对象
3Word.Section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range页脚对象
4Word.Shape.TextFrame.TextRange形状对象

我们需要创建 Word.Find 对象,对上述相关区域分别进行查找替换操作。

Word应用样本

我们假设设计简历模板的输出,并查找且替换对应的关键字,如下图:

其中对应项目的关键字如 {xm}、{xb} 等则为查找且替换的对象,{grzp} 关键字处我们要处理图片的插入。

SqlServer数据表部分设计样本

设计 PersonInfo 数据表如下:

创建脚本如下:

CREATE TABLE [dbo].[PersonInfo]([id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,[sfzh] [varchar](18) NOT NULL,[xm] [nvarchar](50) NOT NULL,[xb] [nvarchar](1) NULL,[grzp] [image] NULL,CONSTRAINT [PK_PersonInfo] PRIMARY KEY CLUSTERED 
([id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],CONSTRAINT [IX_PersonInfo] UNIQUE NONCLUSTERED 
([sfzh] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GOALTER TABLE [dbo].[PersonInfo] ADD  CONSTRAINT [DF_PersonInfo_id]  DEFAULT (newid()) FOR [id]
GO

通过查询 select sfzh,xm,xb,grzp from PersonInfo where id=xxx 得到DataSet,再取 Tables[0]中的数据。 

范例运行环境

操作系统: Windows Server 2019 DataCenter

操作系统上安装 Office Excel 2016

数据库:Microsoft SQL Server 2016

.net版本: .netFramework4.7.1 或以上

开发工具:VS2019  C#

配置Office DCOM

配置方法可参照我的文章《C# 读取Word表格到DataSet》进行处理和配置。

设计实现

组件库引入

实现原理

我们假设查询出表数据,存入对应的变量,其中将二进制字段grzp数据写入到d:\test.jpg生成图片,示例代码如下:

DataTable dt=DataSet.Tables[0];string sfzh = dt.Rows[0]["sfzh"].ToString();
object bt = dt.Rows[0]["grzp"];
byte[] bFile2 = (byte[])bt;
System.IO.File.WriteAllBytes("@d:\test.jpg", bFile2);string xm = dt.Rows[0]["xm"].ToString();
string xb = dt.Rows[0]["xb"].ToString();

 然后我们将其存到二维字符串数组 _repls 里,如下代码:

string[,] _repls = new string[4, 2];
_repls[0, 0] = "{sfzh}";
_repls[0, 1] = sfzh;
_repls[1, 0] = "{xm}";
_repls[1, 1] = xm;
_repls[2, 0] = "{xb}";
_repls[2, 1] = xb;
_repls[3, 0] = "RepalceFromImageFilename_{grzp}";
_repls[3, 1] = "@d:\test.jpg";

其中第一元素存储要查找的关键字,第二元素存储要替换的值。注意:替换图片使用了自定义的RepalceFromImageFilename_ 前缀关键字,则表示值为对应的文件路径。数据准备完毕后,我们将通过遍历数组对 Word 进行查找且替换操作。

查找且替换的核心代码

窗格内容

示例代码如下:

                WordApp.Options.ReplaceSelection = true;Word.Find fnd = WordApp.Selection.Find;for(int i=0;i<_repls.GetLength(0);i++){if (_repls[i, 0] == "" || _repls[i, 0] == null){continue;}fnd.ClearFormatting();string ft = _repls[i, 0];string replaceType = "";if (ft.IndexOf("RepalceFromImageFilename_") == 0){ft = ft.Replace("RepalceFromImageFilename_", "");replaceType = "RepalceFromImageFilename";}else if (ft.IndexOf("RepalceFromImageFilenameNoDelete_") == 0){ft = ft.Replace("RepalceFromImageFilenameNoDelete_", "");replaceType = "RepalceFromImageFilenameNoDelete";}Object findText = ft;Object matchCase = false;Object matchWholeWord = Type.Missing;Object matchWildcards = false;Object matchSoundsLike = false;Object matchAllWordForms = false;Object forward = true;Object wrap =Word.WdFindWrap.wdFindContinue;Object format = false;Object replaceWith ="";Object replace =Type.Missing;;Object matchKashida = Type.Missing;Object matchDiacritics = Type.Missing;Object matchAlefHamza = Type.Missing;Object matchControl = Type.Missing;while(fnd.Execute(ref findText, ref matchCase, ref matchWholeWord,ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWith,ref replace, ref matchKashida, ref matchDiacritics,ref matchAlefHamza, ref matchControl)){string r_f=WordApp.Selection.Font.Name.ToString();if (replaceType == "RepalceFromImageFilename" || replaceType == "RepalceFromImageFilenameNoDelete"){if (File.Exists(_repls[i, 1].ToString())){WordApp.Selection.Range.Select();Word.InlineShape pic = WordApp.Selection.InlineShapes.AddPicture(_repls[i, 1].ToString(), false, true, WordApp.Selection.Range);if (replConfigs != null){string[] cv = replConfigs[ft].Split('|');pic.Width = int.Parse(cv[0]);pic.Height = int.Parse(cv[1]);}if (replaceType == "RepalceFromImageFilename"){File.Delete(_repls[i, 1].ToString());}}}else{WordApp.Selection.Range.Text = _repls[i, 1].ToString();}}}
页眉内容

示例代码如下:

                foreach (Word.Section header in WordDoc.Sections){Word.Range headerRange = header.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;Word.Find fnd = headerRange.Find;for (int i = 0; i < _repls.GetLength(0); i++){if (_repls[i, 0] == "" || _repls[i, 0] == null){continue;}fnd.ClearFormatting();string ft = _repls[i, 0];string replaceType = "";if (ft.IndexOf("RepalceFromImageFilename_") == 0){ft = ft.Replace("RepalceFromImageFilename_", "");replaceType = "RepalceFromImageFilename";}else if (ft.IndexOf("RepalceFromImageFilenameNoDelete_") == 0){ft = ft.Replace("RepalceFromImageFilenameNoDelete_", "");replaceType = "RepalceFromImageFilenameNoDelete";}Object findText = ft;Object matchCase = false;Object matchWholeWord = Type.Missing;Object matchWildcards = false;Object matchSoundsLike = false;Object matchAllWordForms = false;Object forward = true;Object wrap = Word.WdFindWrap.wdFindContinue;Object format = false;Object replaceWith = "";Object replace = Type.Missing; ;Object matchKashida = Type.Missing;Object matchDiacritics = Type.Missing;Object matchAlefHamza = Type.Missing;Object matchControl = Type.Missing;while (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,ref forward, ref wrap, ref format, ref replaceWith, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl)){string r_f = WordApp.Selection.Font.Name.ToString();if (replaceType == "RepalceFromImageFilename" || replaceType == "RepalceFromImageFilenameNoDelete"){if (File.Exists(_repls[i, 1].ToString())){WordApp.Selection.Range.Select();Word.InlineShape pic = WordApp.Selection.InlineShapes.AddPicture(_repls[i, 1].ToString(), false, true, headerRange);if (replaceType == "RepalceFromImageFilename"){File.Delete(_repls[i, 1].ToString());}}}else{headerRange.Text = _repls[i, 1].ToString();}}}}
页脚内容

示例代码如下:

                foreach (Word.Section footer in WordDoc.Sections){Word.Range footerRange = footer.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;Word.Find fnd = footerRange.Find;for (int i = 0; i < _repls.GetLength(0); i++){if (_repls[i, 0] == "" || _repls[i, 0] == null){continue;}fnd.ClearFormatting();string ft = _repls[i, 0];string replaceType = "";if (ft.IndexOf("RepalceFromImageFilename_") == 0){ft = ft.Replace("RepalceFromImageFilename_", "");replaceType = "RepalceFromImageFilename";}else if (ft.IndexOf("RepalceFromImageFilenameNoDelete_") == 0){ft = ft.Replace("RepalceFromImageFilenameNoDelete_", "");replaceType = "RepalceFromImageFilenameNoDelete";}Object findText = ft;Object matchCase = false;Object matchWholeWord = Type.Missing;Object matchWildcards = false;Object matchSoundsLike = false;Object matchAllWordForms = false;Object forward = true;Object wrap = Word.WdFindWrap.wdFindContinue;Object format = false;Object replaceWith = "";Object replace = Type.Missing; ;Object matchKashida = Type.Missing;Object matchDiacritics = Type.Missing;Object matchAlefHamza = Type.Missing;Object matchControl = Type.Missing;while (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,ref forward, ref wrap, ref format, ref replaceWith, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl)){string r_f = WordApp.Selection.Font.Name.ToString();//						WordApp.Selection.Font.Name=r_f;//						WordApp.Selection.Range//						WordApp.Selection.TypeText(_repls[i,1].ToString());if (replaceType == "RepalceFromImageFilename" || replaceType == "RepalceFromImageFilenameNoDelete"){if (File.Exists(_repls[i, 1].ToString())){WordApp.Selection.Range.Select();Word.InlineShape pic = WordApp.Selection.InlineShapes.AddPicture(_repls[i, 1].ToString(), false, true, footerRange);if (replaceType == "RepalceFromImageFilename"){File.Delete(_repls[i, 1].ToString());}}}else{footerRange.Text = _repls[i, 1].ToString();}}}}
形状内容

示例代码如下:

                foreach (Word.Shape shape in WordDoc.Shapes){if (shape.TextFrame.HasText == 0){continue; }Word.Find fnd = shape.TextFrame.TextRange.Find;//Word.Find fnd = WordDoc.Content.Find;for (int i = 0; i < _repls.GetLength(0); i++){if (_repls[i, 0] == "" || _repls[i, 0] == null){continue;}fnd.ClearFormatting();string ft = _repls[i, 0];string replaceType = "";if (ft.IndexOf("RepalceFromImageFilename_") == 0){ft = ft.Replace("RepalceFromImageFilename_", "");replaceType = "RepalceFromImageFilename";}else if (ft.IndexOf("RepalceFromImageFilenameNoDelete_") == 0){ft = ft.Replace("RepalceFromImageFilenameNoDelete_", "");replaceType = "RepalceFromImageFilenameNoDelete";}Object findText = ft;Object matchCase = false;Object matchWholeWord = Type.Missing;Object matchWildcards = false;Object matchSoundsLike = false;Object matchAllWordForms = false;Object forward = true;Object wrap = Word.WdFindWrap.wdFindContinue;Object format = false;Object replaceWith = "";Object replace = Type.Missing; ;Object matchKashida = Type.Missing;Object matchDiacritics = Type.Missing;Object matchAlefHamza = Type.Missing;Object matchControl = Type.Missing;while (fnd.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,ref forward, ref wrap, ref format, ref replaceWith, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl)){string r_f = WordApp.Selection.Font.Name.ToString();if (replaceType == "RepalceFromImageFilename" || replaceType == "RepalceFromImageFilenameNoDelete"){if (File.Exists(_repls[i, 1].ToString())){Word.InlineShape pic = WordApp.Selection.InlineShapes.AddPicture(_repls[i, 1].ToString(), false, true, shape.TextFrame.TextRange);if (replaceType == "RepalceFromImageFilename"){File.Delete(_repls[i, 1].ToString());}}}else{shape.TextFrame.TextRange.Text = _repls[i, 1].ToString();}}}}

小结

1、示例代码是冗余的写法,在实际应用中我们需要进行优化。

2、添加图片后,代码默认是使用完毕后,删除图片文件以释放空间,我们自定义了 RepalceFromImageFilenameNoDelete_ 前缀关键字,表示使用完毕后不进行文件删除。

3、示例代码中 Word 表示 using Word=Microsoft.Office.Interop.Word; 的引用。

4、示例代码 WordDoc 表示对 Word.Document 的引用。

示例代码我们提供了操作的关键方法,这里仅作参考,其它代码不再做展示,欢迎大家评论指教!

这篇关于C# 操作 Word 全域查找且替换(含图片对象)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/859545

相关文章

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指