一、创建模板,使用MergeField绑定数据
二、使用数组提供数据源
三、模板上使用书签,插入标记位置
四、创建循环数据的模板,这里的循环类似于页面的for循环结构,不局限于形式table
五、使用Datatable提供数据源(配合四)
六、绑定带有子循环的数据模块
七、使用DataSet提供数据源
八、在书签位置插入另一个文档的内容
向word文档中插入图片(方法一)
xiangword文档中插入图片(方法二)
代码技巧:
创建OOXML图表
数据标签的设置?
二、使用Aspose将图像转换为PDF
文档合并?
publicActionResultdemo2(){try{Documentdoc =newDocument(); doc.RemoveAllChildren();intrecordCount = 5;varguid =Guid.NewGuid().ToString();stringoutPath = Server.MapPath("~/ReportModels/"+ guid +".docx");for(inti = 1; i <= recordCount; i++){stringModelPath = Server.MapPath("~/ReportModels/模板.docx");//DocumentsrcDoc =newDocument(ModelPath);DocumentBuilderbuilder =newDocumentBuilder(srcDoc);Dictionary dic =newDictionary(); dic.Add("demo1","活动demo"+i); dic.Add("demo2","危险源demo"+i); dic.Add("demo3","编号demo"+i); dic.Add("demo4","分布demo"+i); dic.Add("demo5","事件demo"+i);this.MailByDic(dic, builder);DataTabletable1 =newDataTable("Table"); table1.Columns.Add("demo6"); table1.Columns.Add("demo7"); table1.Rows.Add(newobject[]{"测试1","测试1"+i}); table1.Rows.Add(newobject[]{"测试2","测试3"+i}); table1.Rows.Add(newobject[]{"测试3","测试3"+i}); table1.Rows.Add(newobject[]{"测试3","测试3"+i}); srcDoc.MailMerge.ExecuteWithRegions(table1);//doc.AppendDocument(srcDoc,ImportFormatMode.UseDestinationStyles);}doc.Save(outPath,SaveFormat.Docx);returnJson(new{IsSuccess =true, Message ="报告生成成功"},JsonRequestBehavior.AllowGet);}catch(Exceptionex){LogHelper.WriteLog("DemoController.demo2", ex);returnnull;}}/// /// 通过字典执行word渲染/// /// /// publicvoidMailByDic(Dictionary dic,DocumentBuilderbuilder){foreach(stringkeyindic.Keys){builder.MoveToMergeField(key); builder.Write(dic[key]);}}