当前位置: 首页 > news >正文

重庆建网站seo手机端排名软件

重庆建网站,seo手机端排名软件,临朐网站建设定制,界首市合肥网络推广外包目录 应用需求 设计 范例运行环境 配置Office DCOM 实现代码 组件库引入 ​核心代码 调用示例 小结 应用需求 在我的文章《C# 读取多条数据记录导出到 Word 标签模板》里,讲述读取多条数据记录结合 WORD 标签模板输出文件的功能,原有输出图片的…

目录

应用需求

设计

范例运行环境

配置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 列表指令。

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);}

调用示例

示例代码如下:

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 后,可以进行环绕文字的操作,如下:

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相关的关键方法,这里仅作参考,欢迎大家评论指教!

http://www.dinnco.com/news/76476.html

相关文章:

  • 个人网站备案名称举例竞价托管sem服务
  • 林州网站建设策划seo结算系统
  • 站内推广的方式有哪些windows优化大师怎么卸载
  • 温州网站开发多少钱百度资讯
  • 网站的建设周期与阶段竞价sem托管
  • 高端网站设计制作的网络推广学校
  • 网站设置文件夹权限设置互联网论坛
  • 自己人网站建设百度百度网址大全
  • 做网站的把网站写成一行打开百度网页版
  • 网站建设学习学校百度指数热度榜
  • 旅游网站建设方案互联网推广平台
  • 为什么做pc网站舆情网站
  • 北京app制作哪家好郑州seo询搜点网络效果佳
  • wordpress英文企业模板下载河南网站建设优化技术
  • 临安做网站知乎seo排名的搜软件
  • 网站建设 套餐外贸网站建设流程
  • 外贸网站优化谷歌关键词排名外包国内快速建站
  • wordpress建售卖产品的网站广告公司推广软文
  • flash制作网站界面郑州做网站推广哪家好
  • 湛江网站建设核心关键词举例
  • 专业建设网站公司哪家好网络营销公司是做什么的
  • 现在最火的电商平台是什么网站优化排名易下拉效率
  • 文联网站建设方案平台推广公司
  • 金利福珠宝的网站建设理念青岛网
  • 做网站 域名 网站 空间免费建设个人网站
  • 地产网站设计代做seo排名
  • wordpress图片库seo专业推广
  • 万能网站网址下载seo推广优化多少钱
  • 怎样申请自己的网站百度ai人工智能
  • app怎么制作的网站优化策略