C# 读取多条数据记录导出到 Word标签模板之图片输出改造

目录

应用需求

设计

范例运行环境

[配置Office DCOM](#配置Office DCOM)

实现代码

组件库引入

核心代码

调用示例

小结


应用需求

在我的文章《C# 读取多条数据记录导出到 Word 标签模板》里,讲述读取多条数据记录结合 WORD 标签模板输出文件的功能,原有输出图片的约定是在 WORD 模板文件中借助书签设置进行输出,如下图:

该书签名称代表了一条指令,格式为(输出关键字_图片宽度_图片高度)。如图中书签名称设置,当系统遇到 ds_qz 关键字时,输出按后继设置进行,图片宽度为100、图片高度为 50。

现有一模板需求如下图:

我们需要在考官评语和考官本人签字位置输出手写图片,原有的书签模式输出会遇到一些问题:

(1)只能为嵌入式,位置输出无法设置,无法进一步调整到较优呈现位置。

(2)只能插入式输出,无法实现嵌入式如文字浮动、环绕效果。

(3)需要在无关位置插入书签,否则会在输出关键字时被替换掉,而无法识别配置。

(4)书签的名称不能输入一些特定字符,如%,减号等,给配置带来一些麻烦。

(5)如果在文档里真需要设置书签,容易造成混乱。

设计

设计提供一个 ArrayList 类型参数取代书签模式的设计,其格式为 (输出关键字_图片宽度_图片高度_图片 Left 相对值_图片 Top 相对值 )。如添加 ds_qz_100_50_500_-20 字符串,当系统遇到 ds_qz 关键字时,输出按后继设置进行,图片宽度为100、图片高度为 50、图片 Left 为500、图片 Top 为 -20。后四个参数如果设置null则表示忽略,ds_qz_null_null_500_-20 ,则表示忽略宽度和高度设置,即表示原始大小输出。

定位到的图片输出,采取浮动于文字下方的处理。

范例运行环境

操作系统: Windows Server 2019 DataCenter

操作系统上安装 Office Word 2016

数据库:Microsoft SQL Server 2016

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

开发工具:VS2019 C#

配置Office DCOM

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

实现代码

组件库引入

核心代码

public string DataTableToWord(string _filename,string _repeatKey,object _dataset,ArrayList DataSetPictureConfig),该方法提供4个参数,WORD模板文件名、自定义关键字、System.Data.DataSet,配置图片用的 ArrayList 列表指令。

cs 复制代码
public void DataTableToWord(string _filename,string _repeatKey,object _dataset,ArrayList DataSetPictureConfig)
        {
            if (DataSetPictureConfig == null) { DataSetPictureConfig = new ArrayList(); }

            Object Nothing = System.Reflection.Missing.Value;
            object filename = _filename;
            //创建一个名为WordApp的组件对象
            Word.Application WordApp = new Word.Application();
            //创建一个名为WordDoc的文档对象
            WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            WordDoc.SpellingChecked = false;//关闭拼写检查

            WordDoc.ShowSpellingErrors = false;//关闭显示拼写错误提示框

				WordApp.Selection.WholeStory();
				WordApp.Selection.Cut();
			 	DataSet ds=(DataSet)_dataset;
				System.Data.DataTable  dt=ds.Tables[0];
				for(int i=0;i<dt.Rows.Count;i++)
				{
					WordApp.Selection.Paste();
					for(int j=0;j<dt.Columns.Count;j++)
					{
						
						string _repKey=_repeatKey+dt.Columns[j].ColumnName.ToString();
						string _repValue=string.Format("{0}",dt.Rows[i][j].ToString());

						bool isPhoto=false;
						if(dt.Columns[j].DataType==typeof(System.Byte[]))
						{
							isPhoto=true;
							_repValue="";
						}


						
						WordApp.Options.ReplaceSelection=true;
						Word.Find fnd = WordApp.Selection.Find;
						fnd.ClearFormatting();

						Object findText = _repKey;
						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.Range.Text=_repValue;
							if(isPhoto==true)
							{
								string _jpgfile=_path+System.Guid.NewGuid()+".jpg";
                                if (dt.Rows[i][j] == System.DBNull.Value)
                                {
                                    continue;
                                }
								byte[] filedata = (byte[])dt.Rows[i][j];
								System.IO.MemoryStream ms = new MemoryStream(filedata);
								System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);
								img1.Save(@_jpgfile);
								ms.Close();
                                Word.InlineShape pic= WordApp.Selection.InlineShapes.AddPicture(@_jpgfile,false,true);
                                foreach (string bm in  DataSetPictureConfig)
								{
									string _findkey=_repKey+"_";

                                    int _f1 =bm.IndexOf(_findkey);
									if(_f1==0 && bm.Length>(_findkey.Length))
									{
                                        string[] _paras=bm.Substring(_findkey.Length,bm.Length-_findkey.Length).Split('_');
										if(_paras.GetLength(0)>1){
                                            if (_paras[0] != "null")
                                            {
                                                int def_width = int.Parse(_paras[0]);
                                                pic.Width = def_width;
                                            }
                                            if (_paras[1] != "null")
                                            {
                                                int def_height = int.Parse(_paras[1]);
                                                pic.Height = def_height;
                                            }

                                        }
                                    }
								}
                                Word.Shape pic2 = pic.ConvertToShape();
                                pic2.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;
                                pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
                                pic2.Left = 0;
                                pic2.Top = 0;
                                foreach (string bm in DataSetPictureConfig)
                                {
                                    string _findkey = _repKey + "_";

                                    int _f1 = bm.IndexOf(_findkey);
                                    if (_f1 == 0 && bm.Length > (_findkey.Length))
                                    {
                                        string[] _paras = bm.Substring(_findkey.Length, bm.Length - _findkey.Length).Split('_');
                                        if (_paras.GetLength(0) > 3)
                                        {
                                            if (_paras[2] != "null")
                                                pic2.Left = float.Parse(_paras[2]);
                                            if (_paras[3] != "null")
                                                pic2.Top =float.Parse(_paras[3]);

                                        }
                                    }
                                }
								File.Delete(@_jpgfile);
							}
					
						}
					}
					
					object dummy = System.Reflection.Missing.Value;
					object what = Word.WdGoToItem.wdGoToLine;
					object which = Word.WdGoToDirection.wdGoToLast;
					object count = System.Reflection.Missing.Value;
//					WordApp.Selection.GoTo(ref oGoToItem, ref oGoToLast, ref Nothing, ref Nothing);
					WordApp.Selection.GoTo(ref what, ref which, ref count, ref dummy);
					//default 表示每行记录之间插入分页符,最后一行时不再插入分页符,以免造成多余一空白页
    				if(i!=dt.Rows.Count-1)
						{
							object ib = Word.WdBreakType.wdPageBreak; 
							WordApp.Selection.InsertBreak(ref ib);
						}
					}
	
             }
			WordDoc.Save();
            WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
			//关闭WordApp组件对象
			WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

}

调用示例

示例代码如下:

cs 复制代码
DataSet rv = GetDataSet("select * from tabble");

ArrayList picconfig = new ArrayList();   //可配置每个图片字段的大小及位置
picconfig.Add("ds_kg_sign_100_50_500_-20");   //格式为: key_width_height_left_top
string ModuleFile = "d:\\bfile\\word\\面试评分表.docx";  //假设的模板文件
string _lastfile = DataTableToWord(ModuleFile,"ds_",ds,picconfig);

MessageBox.Show(string.Format("已成功导出文件:{0}", _lastfile));

运行结果预览如下图:

小结

核心代码中需要将 Word.InlineShape 转换为 Word.Shape 后,可以进行环绕文字的操作,如下:

cs 复制代码
Word.Shape pic2 = pic.ConvertToShape();
pic2.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;
pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
pic2.Left = 0;
pic2.Top = 0;

使用 ConvertToShape() 方法转换为 pic2 , pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind; 可以浮动于文字下方,然后初始图片的 Left 和 Top ,否则有可能无法正常显示位置。

我们可以根据自己的实际情况设置环绕格式,可参阅读如下地址:

https://learn.microsoft.com/zh-cn/office/vba/api/word.wdwraptypemerged

这些代码我们提供了一些操作WORD相关的关键方法,这里仅作参考,欢迎大家评论指教!

相关推荐
曹牧3 分钟前
Java:Foreach语法糖
java·开发语言·python
盼哥PyAI实验室4 分钟前
Python验证码处理实战:从12306项目看验证码识别的技术演进
开发语言·网络·python
222you8 分钟前
SpringIOC的注解开发
java·开发语言
William_cl9 分钟前
【CSDN 专栏】C# ASP.NET Razor 视图引擎实战:.cshtml 从入门到避坑(图解 + 案例)
开发语言·c#·asp.net
charlie11451419113 分钟前
深入理解CC++的编译与链接技术9:动态库细节
c语言·开发语言·c++·学习·动态库
席之郎小果冻13 分钟前
【03】【创建型】【聊一聊,单例模式】
开发语言·javascript·单例模式
god0017 分钟前
Selenium等待判断元素页面加载完成
java·开发语言
isyoungboy19 分钟前
c++使用win新api替代DirectShow驱动uvc摄像头,可改c#驱动
开发语言·c++·c#
Dxy123931021621 分钟前
python如何去掉字符串中最后一个字符
开发语言·python
云和数据.ChenGuang33 分钟前
`post_max_size`、`max_execution_time`、`max_input_time` 是 **PHP 核心配置参数**
开发语言·mysql·php·zabbix·mariadb