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

简介:

关于全域查找且替换

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

序号

对象

说明

1

Word.Appication.Selection 窗格对象

2

Word.Section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range

页眉对象

3

Word.Section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range

页脚对象

4

Word.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]
GO
 
ALTER 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 的引用。


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

————————————————


                           版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

                     

原文链接:https://blog.csdn.net/michaelline/article/details/137143634

相关文章
|
3月前
|
数据采集 JavaScript C#
C#图像爬虫实战:从Walmart网站下载图片
C#图像爬虫实战:从Walmart网站下载图片
|
2月前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
125 65
|
1月前
|
JSON 程序员 C#
使用 C# 比较两个对象是否相等的7个方法总结
比较对象是编程中的一项基本技能,在实际业务中经常碰到,比如在ERP系统中,企业的信息非常重要,每一次更新,都需要比较记录更新前后企业的信息,直接比较通常只能告诉我们它们是否指向同一个内存地址,那我们应该怎么办呢?分享 7 个方法给你!
|
1月前
|
XML C# 开发工具
C# 删除Word文档中的段落
【11月更文挑战第3天】本文介绍了两种方法来操作 Word 文档:一是使用 `Microsoft.Office.Interop.Word` 库,适用于 Windows 环境下操作 Word 文档,需引用相应库并在代码中引入命名空间;二是使用 Open XML SDK,适用于处理 .docx 格式的文档,通过引用 `DocumentFormat.OpenXml` 库实现。文中提供了示例代码,展示了如何打开、删除段落并保存文档。
|
3月前
|
C# 数据安全/隐私保护
C# 一分钟浅谈:类与对象的概念理解
【9月更文挑战第2天】本文从零开始详细介绍了C#中的类与对象概念。类作为一种自定义数据类型,定义了对象的属性和方法;对象则是类的实例,拥有独立的状态。通过具体代码示例,如定义 `Person` 类及其实例化过程,帮助读者更好地理解和应用这两个核心概念。此外,还总结了常见的问题及解决方法,为编写高质量的面向对象程序奠定基础。
28 2
|
4月前
|
数据库
C#Winform使用NPOI获取word中的数据
C#Winform使用NPOI获取word中的数据
200 2
|
7月前
|
C#
C#的类和对象的概念学习案例刨析
【5月更文挑战第17天】C#是一种面向对象的语言,以类和对象为核心。类作为对象的模板,定义了属性(如Name, Age)和行为(如Greet)。对象是类的实例,可设置属性值。封装通过访问修饰符隐藏实现细节,如Customer类的私有name字段通过Name属性访问。继承允许新类(如Employee)从现有类(Person)继承并扩展。多态让不同对象(如Circle, Square)共享相同接口(Shape),实现抽象方法Area,提供灵活的代码设计。
69 1
|
7月前
|
存储 算法 C#
C# 生成指定图片的缩略图
C# 生成指定图片的缩略图
|
7月前
|
C# 开发工具 数据安全/隐私保护
C# 给图片添加文字水印
C# 给图片添加文字水印
|
7月前
|
开发框架 .NET C#
C# 自动填充文字内容到指定图片
C# 自动填充文字内容到指定图片