![Aspose Word模板使用总结_第1页](http://file2.renrendoc.com/fileroot_temp3/2021-7/19/59d8a110-6195-44bb-aced-1b48656d86a8/59d8a110-6195-44bb-aced-1b48656d86a81.gif)
![Aspose Word模板使用总结_第2页](http://file2.renrendoc.com/fileroot_temp3/2021-7/19/59d8a110-6195-44bb-aced-1b48656d86a8/59d8a110-6195-44bb-aced-1b48656d86a82.gif)
![Aspose Word模板使用总结_第3页](http://file2.renrendoc.com/fileroot_temp3/2021-7/19/59d8a110-6195-44bb-aced-1b48656d86a8/59d8a110-6195-44bb-aced-1b48656d86a83.gif)
![Aspose Word模板使用总结_第4页](http://file2.renrendoc.com/fileroot_temp3/2021-7/19/59d8a110-6195-44bb-aced-1b48656d86a8/59d8a110-6195-44bb-aced-1b48656d86a84.gif)
![Aspose Word模板使用总结_第5页](http://file2.renrendoc.com/fileroot_temp3/2021-7/19/59d8a110-6195-44bb-aced-1b48656d86a8/59d8a110-6195-44bb-aced-1b48656d86a85.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Aspose Word模板使用总结命名空间:1. using Aspose.Words;2. using Aspose.Words.Saving;3. using System.IO;4. using System.Data;添加dll:链接:1.创建word模版,使用MergeFeild绑定数据 新建一个Word文档,命名为Template.doc 注意:这里并不是输入”和“”就可以了,而是必须在菜单的插入文档部件域”找到MergeField并输入相应的域名2.使用数组提供数据源1. string tempPath = Server.MapPath(/Docs/Temp/Template.d
2、oc);2. string outputPath = Server.MapPath(/Docs/Output/Template.doc);3. /载入模板4. var doc = new Document(tempPath);5. /提供数据源6. String fieldNames = new String UserName, Gender, BirthDay, Address;7. Object fieldValues = new Object 张三, 男, 1988-09-02, 陕西咸阳;8. /合并模版,相当于页面的渲染9. doc.MailMerge.Execute(fieldNa
3、mes, fieldValues);10. /保存合并后的文档11. doc.Save(outputPath);12. /在WebForm中,保存文档到流中,使用Response.?BinaryWrite输出该文件13. var docStream = new MemoryStream();14. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);15. Response.ContentType = application/msword;16. Response.AddHeader(content-disposi
4、tion, attachment; filename=Template.doc);17. Response.BinaryWrite(docStream.ToArray();18. Response.End();19. /在MVC中采用,保存文档到流中,使用base.File输出该文件20. var docStream = new MemoryStream();21. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);22. return base.File(docStream.ToArray(), applica
5、tion/msword,Template.doc);3.创建循环数据的模版,这里的循环数据类似页面的for结构,不拘泥于形式table TableStart:UserList 姓名:UserName TableEnd:UserList 4.使用DataTable提供数据源1. /创建名称为UserList的DataTable2. DataTable table=new DataTable(UserList);3. table.Columns.Add(UserName);4. table.Columns.Add(Gender);5. table.Columns.Add(BirthDay);6.
6、table.Columns.Add(Address);7. /-8. /载入模板9. var doc = new Document(tempPath);10. /提供数据源11. var datatable= GetDataTable();12. /合并模版,相当于页面的渲染13. doc.MailMerge.ExecuteWithRegions(datatable);14. var docStream = new MemoryStream();15. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);16. r
7、eturn base.File(docStream.ToArray(), application/msword,Template.doc);5.绑定带有子循环数据模版6.使用DataSet提供数据源1. /用户表结构2. DataTable table = new DataTable(UserList);3. table.Columns.Add(new DataColumn(Id, typeof(int);4. table.Columns.Add(UserName);5. table.Columns.Add(Gender);6. table.Columns.Add(BirthDay);7. t
8、able.Columns.Add(Address);8. /分数表结构9. DataTable table = new DataTable(ScoreList);10. table.Columns.Add(new DataColumn(UserId, typeof(int);11. table.Columns.Add(Name);12. table.Columns.Add(Score);13. /-14. /载入模板15. var doc = new Document(tempPath);16. /提供数据源17. DataSet dataSet = new DataSet();18. var
9、 userTable= GetUserDataTable();19. var userScoreTable= GetUserScoreDataTable();20. dataSet.Tables.Add(userTable);21. dataSet.Tables.Add(userScoreTable);22. dataSet.Relations.Add(new DataRelation(ScoreListForUser,userTable.ColumnsId,?userScoreTable.ColumnsUserId);23. /合并模版,相当于页面的渲染24. doc.MailMerge.E
10、xecuteWithRegions(dataSet);25. var docStream = new MemoryStream();26. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);27. return base.File(docStream.ToArray(), application/msword,Template.doc);7.模版上使用书签,插入标记位置选中文档中的文字,在菜单的插入书签”指定书签的名称,排序依据选定为位置,添加一个新书签。选中的文字为书签的Text属性,这里是为了方便查看。也可以
11、直接插入一个书签并指定位置,只是不明显。8.在书签位置插入另一个文档的内容1. /载入模板2. var doc = new Document(tempPath);3. var doc1 = new Document(tempPath1);/新文档4. /找到名称为PositionFlag的书签5. var bookmark= doc.Range.BookmarksPositionFlag;6. /清空书签的文本7. bookmark.Text = ;8. /使用DocumentBuilder对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个段落,插入空白页,追加或另一个word
12、文件的内容等。9. var builder = new DocumentBuilder(doc);10. /定位到指定位置进行插入操作11. builder.MoveToBookmark(PositionFlag);12. /在PositionFlag书签对应的位置,插入另一个文档的内容。13. /InsertDocument方法可以在14. InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);9.创建word模版,使用MergeFeild插入图片10.插入图片示例1. string tempPath = Server.MapPath
13、(/Docs/Temp/Template.doc);2. string logoPath = Server.MapPath(/Content/logo.jpg);3. var doc = new Document(tempPath); /载入模板4. /提供数据源5. String fieldNames = new String logo, Gender, BirthDay, Address,Logo ;6. Object fieldValues = new Object 张三, 男, 1988-09-02, 陕西咸阳,logoPath ;7. /增加处理图片大小程序8. /doc.MailM
14、erge.FieldMergingCallback = new HandleMergeFieldInsertDocument();9. /合并模版,相当于页面的渲染10. doc.MailMerge.Execute(fieldNames, fieldValues);11.12. /在MVC中采用,保存文档到流中,使用base.File输出该文件13. var docStream = new MemoryStream();14. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);15. return base.Fi
15、le(docStream.ToArray(), application/msword, Template.doc); 效果如下:增加图片大小处理的程序1. /Aspose.Word提供了一个类似Handler的功能,IFieldMergingCallback允许我们动态的处理MergeField2. void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) /处理文本3. void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) /处理图片4.
16、/这里我们处理图片写了一个自定义的类实现5. class HandleMergeFieldInsertDocument : IFieldMergingCallback6. 7. /文本处理在这里,如果写在这一块,则不起作用8. void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)9. 10.11. 12. /图片处理在这里13. void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)14. 15. if (args.DocumentFiel
17、dName.Equals(Logo)16. 17. / 使用DocumentBuilder处理图片的大小18. DocumentBuilder builder = new DocumentBuilder(args.Document);19. builder.MoveToMergeField(args.FieldName);20.21. Shape shape = builder.InsertImage(args.FieldValue.ToString();22.23. / 设置x,y坐标和高宽.24. shape.Left = 0;25. shape.Top = 0;26. shape.Wid
18、th = 60;27. shape.Height = 80;28. 29. 30. 效果如下:11.向模版插入Html这里的家乡简介使用html格式12.插入html示例1. string tempPath = Server.MapPath(/Docs/Temp/Template.doc);2. string descHtml = ;/这里是html文本,由于太长略去3. var doc = new Document(tempPath); /载入模板4. /提供数据源5. String fieldNames = new String UserName, Gender, BirthDay, Ad
19、dress,Desc;6. Object fieldValues = new Object 张三, 男, 1988-09-02, 陕西咸阳, descHtml;7. /增加处理html程序8. doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();9. /合并模版,相当于页面的渲染10. doc.MailMerge.Execute(fieldNames, fieldValues);11. /在MVC中采用,保存文档到流中,使用base.File输出该文件12. var docStream = new MemoryStream();13. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc);14. return base.File(docStream.ToArray(), application/mswor
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024公路运输合同的范本
- 婚姻散场离婚协议书样本
- 假山制作双方合同(2024版)
- 挡墙工程全面施工合同
- 小学数学人教版六年级上册第五单元练习题(含答案)
- 本车库买卖合同(2024版)
- 物业管理服务项目绩效评估报告
- 超细矿粉技改项目可行性研究报告模板-立项备案
- 建筑工程识图(中高级-建筑水暖) 课件 任务2.2 识读给水工程施工图
- 专项提分卷一语音与汉字练习
- 宜宾国企公开招聘综合能力测试题
- 当事人银行结算账户确认书模板
- 2023年中国软件行业基准数据SSM-BK-202310
- 半导体工艺风险点
- 儿科川崎病一病一品护理框架护理方案
- 痛风科普讲座
- 煤矿掘进现场管理课件
- 内蒙古自治区呼和浩特市四中学2024届数学七下期末统考试题含解析
- DB37T 4691-2024农业面源污染负荷估算技术规程
- 《县域经济发展》课件
- 芯片生产工艺流程
评论
0/150
提交评论