一丽已、創(chuàng)建模板,使用MergeField綁定數(shù)據(jù)
二买决、使用數(shù)組提供數(shù)據(jù)源
三沛婴、模板上使用書簽,插入標(biāo)記位置
四督赤、創(chuàng)建循環(huán)數(shù)據(jù)的模板嘁灯,這里的循環(huán)類似于頁(yè)面的for循環(huán)結(jié)構(gòu),不局限于形式table
五躲舌、使用Datatable提供數(shù)據(jù)源(配合四)
六丑婿、綁定帶有子循環(huán)的數(shù)據(jù)模塊
七、使用DataSet提供數(shù)據(jù)源
八孽糖、在書簽位置插入另一個(gè)文檔的內(nèi)容
向word文檔中插入圖片(方法一)
xiangword文檔中插入圖片(方法二)
代碼技巧:
創(chuàng)建OOXML圖表
數(shù)據(jù)標(biāo)簽的設(shè)置枯冈?
二、使用Aspose將圖像轉(zhuǎn)換為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","活動(dòng)demo"+i);????????????????????dic.Add("demo2","危險(xiǎn)源demo"+i);????????????????????dic.Add("demo3","編號(hào)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[]{"測(cè)試1","測(cè)試1"+i});????????????????????table1.Rows.Add(newobject[]{"測(cè)試2","測(cè)試3"+i});????????????????????table1.Rows.Add(newobject[]{"測(cè)試3","測(cè)試3"+i});????????????????????table1.Rows.Add(newobject[]{"測(cè)試3","測(cè)試3"+i});????????????????????srcDoc.MailMerge.ExecuteWithRegions(table1);//doc.AppendDocument(srcDoc,ImportFormatMode.UseDestinationStyles);}doc.Save(outPath,SaveFormat.Docx);returnJson(new{IsSuccess?=true,?Message?="報(bào)告生成成功"},JsonRequestBehavior.AllowGet);}catch(Exceptionex){LogHelper.WriteLog("DemoController.demo2",?ex);returnnull;}}///?///?通過(guò)字典執(zhí)行word渲染///?///?///?publicvoidMailByDic(Dictionary?dic,DocumentBuilderbuilder){foreach(stringkeyindic.Keys){builder.MoveToMergeField(key);????????????????builder.Write(dic[key]);}}