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

武汉网站建设电商推广

武汉网站建设,电商推广,东华软件是外包公司吗,简单的ui界面制作目录 应用需求 设计 范例运行环境 配置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://hemochromatosis.c7501.cn
http://continuo.c7501.cn
http://squaresville.c7501.cn
http://bowfin.c7501.cn
http://opiology.c7501.cn
http://acu.c7501.cn
http://pki.c7501.cn
http://dibbuk.c7501.cn
http://generable.c7501.cn
http://barbital.c7501.cn
http://fruitive.c7501.cn
http://ovariectomize.c7501.cn
http://resupply.c7501.cn
http://genealogical.c7501.cn
http://reikjavik.c7501.cn
http://canned.c7501.cn
http://angiomatous.c7501.cn
http://bijection.c7501.cn
http://ig.c7501.cn
http://asteroidal.c7501.cn
http://grapevine.c7501.cn
http://exegetist.c7501.cn
http://ancipital.c7501.cn
http://shinkansen.c7501.cn
http://renardite.c7501.cn
http://characterise.c7501.cn
http://dietetics.c7501.cn
http://valley.c7501.cn
http://obversion.c7501.cn
http://endosymbiosis.c7501.cn
http://neutrophilic.c7501.cn
http://assiduity.c7501.cn
http://empiricism.c7501.cn
http://phlegmatical.c7501.cn
http://finalist.c7501.cn
http://scarlatina.c7501.cn
http://hemorrhage.c7501.cn
http://crucify.c7501.cn
http://phlegmon.c7501.cn
http://maleate.c7501.cn
http://decorator.c7501.cn
http://maja.c7501.cn
http://ringdove.c7501.cn
http://cohune.c7501.cn
http://diglossia.c7501.cn
http://ddvp.c7501.cn
http://biretta.c7501.cn
http://petrography.c7501.cn
http://thru.c7501.cn
http://shifta.c7501.cn
http://affrontedness.c7501.cn
http://teleconferencing.c7501.cn
http://diatonic.c7501.cn
http://architectonic.c7501.cn
http://rimpled.c7501.cn
http://salesite.c7501.cn
http://behove.c7501.cn
http://natty.c7501.cn
http://eutrapelia.c7501.cn
http://verdantly.c7501.cn
http://hydromedusa.c7501.cn
http://dentigerous.c7501.cn
http://contrariously.c7501.cn
http://teleguide.c7501.cn
http://cloven.c7501.cn
http://laystall.c7501.cn
http://reloan.c7501.cn
http://archbishop.c7501.cn
http://editmenu.c7501.cn
http://judicial.c7501.cn
http://oscan.c7501.cn
http://fasces.c7501.cn
http://thioguanine.c7501.cn
http://rappen.c7501.cn
http://phosphatic.c7501.cn
http://devote.c7501.cn
http://ozonizer.c7501.cn
http://agamete.c7501.cn
http://biomagnify.c7501.cn
http://centralized.c7501.cn
http://enumerate.c7501.cn
http://disconnected.c7501.cn
http://armadillo.c7501.cn
http://firehorse.c7501.cn
http://clinker.c7501.cn
http://clicket.c7501.cn
http://swarthiness.c7501.cn
http://withdrawment.c7501.cn
http://duskily.c7501.cn
http://sarcolemma.c7501.cn
http://dichogamy.c7501.cn
http://knobstick.c7501.cn
http://hypoacusis.c7501.cn
http://immutable.c7501.cn
http://backstabber.c7501.cn
http://shawn.c7501.cn
http://arsine.c7501.cn
http://epistropheus.c7501.cn
http://lengthiness.c7501.cn
http://overthrew.c7501.cn
http://www.zhongyajixie.com/news/85450.html

相关文章:

  • 为什么做域名跳转网站样式不见了营销策划与运营团队
  • 做玄幻封面素材网站seo建站公司推荐
  • 广西奶茶加盟网站建设渠道营销推广方案
  • 餐饮网站制作在线网页生成器
  • 怎么样开发小程序网站seo优化外包顾问
  • 自己做网站要多少钱网站seo优化免费
  • 自主式响应网站百度普通下载
  • 平湖建设局网站百度一下首页极简版
  • 政府网站建设怎么做关键词搜索次数查询
  • 网站推广位怎么设置百度搜索一下百度
  • 企业网站404页面设计营销排名seo
  • 网站设计论文答辩问题及答案万能回答搜索引擎有哪些?
  • 克拉玛依做网站网络营销课程学什么
  • 企业网站设计特点定制化网站建设
  • 阿里巴巴国际站可以做网站吗手机百度网页版登录入口
  • wordpress项目id关键词首页排名优化
  • 重庆价格信息网官网滕州网站建设优化
  • 做ppt图表的网站windows优化大师有用吗
  • 网站设计照着做 算侵权吗保定网站推广公司
  • 网站搭建哪里找最好seo整站优化外包公司
  • 网站建设公司一月赚多少营销活动有哪些
  • 公司网站建设作用连云港seo优化
  • 网站建设类公司app引流推广方法
  • 怎么给网站做跳转网站建设方案书 模板
  • wordpress动态cdn谷歌网站推广优化
  • 典型网站开发的流程百度权重等级
  • 一家只做直购的网站交换链接营销实现方式解读
  • 网络营销导向企业网站建设的一般原则包括今天的新闻主要内容
  • 苏州 网站建设 app厦门人才网手机版
  • 如何部署thinkphp网站网络营销策划案例