第二篇:Aspose Word模板使用总结
Aspose Word 模板使用总结命名空间:1. 2. 3. 4.using Aspose.Words; using Aspose.Words.Saving; using System.IO; using System.Data;添加 dll: 链接:/s/1pJG899T 密码:bv3k1.创建 word 模版,使用 MergeFeild 绑定数据新建一个 Word 文档,命名为 Template.doc注意: 这里并不是输入" 《” 和 “》 ” 就可以了, 而是必须在菜单的"插入→文档部件→域” 找到 MergeField 并输入相应的域名
2.使用数组提供数据源1. 2. 3. 4. 5. 6. 7. 8. 9.string tempPath = Server.MapPath("~/Docs/Temp/Template.doc"); string outputPath = Server.MapPath("~/Docs/Output/Template.doc"); //载入模板 var doc = new Document(tempPath); //提供数据源 String[] fieldNames = new String[] {"UserName", "Gender", "BirthDay", "Address"}; Object[] fieldValues = new Object[] {"张三", "男", "1988-09-02", "陕西咸阳"}; //合并模版,相当于页面的渲染 doc.MailMerge.Execute(fieldNames, 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-disposition", "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(), "application/msword","Template.doc");3.创建循环数据的模版, 这里的循环数据类似页面的 for 结构, 不拘泥于形式 table?TableStart:UserList? 姓名:?UserName? ?TableEnd:UserList?4.使用 DataTable 提供数据源1. 2. 3. 4. 5. 6. 7. 8. 9.//创建名称为 UserList 的 DataTable DataTable table=new DataTable("UserList"); table.Columns.Add("UserName"); table.Columns.Add("Gender"); table.Columns.Add("BirthDay"); table.Columns.Add("Address"); //---------------------------------------------------------------------------------------------------//载入模板 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. return base.File(docStream.ToArray(), "application/msword","Template.doc");5.绑定带有子循环数据模版6.使用 DataSet 提供数据源1. 2. 3. 4. 5. 6. 7. 8. 9.//用户表结构 DataTable table = new DataTable("UserList"); table.Columns.Add(new DataColumn("Id", typeof(int))); table.Columns.Add("UserName"); table.Columns.Add("Gender"); table.Columns.Add("BirthDay"); table.Columns.Add("Address"); //分数表结构 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 userTable= GetUserDataTable();
19. var userScoreTable= GetUserScoreDataTable(); 20. dataSet.Tables.Add(userTable); 21. dataSet.Tables.Add(userScoreTable); 22. dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"],?userScoreTable.Columns["UserId"])); 23. //合并模版,相当于页面的渲染 24. doc.MailMerge.ExecuteWithRegions(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 属性,这里是为了方便查看。也可以直接插入一个书签并指定位置,只是不明显。8.在书签位置插入另一个文档的内容1. 2. 3.//载入模板 var doc = new Document(tempPath); var doc1 = new Document(tempPath1);//新文档
4. 5. 6. 7. 8.//找到名称为 PositionFlag 的书签 var bookmark= doc.Range.Bookmarks["PositionFlag"]; //清空书签的文本 bookmark.Text = ""; //使用 DocumentBuilder 对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个 段落,插入空白页,追加或另一个 word 文件的内容等。9.var builder = new DocumentBuilder(doc);10. //定位到指定位置进行插入操作 11. builder.MoveToBookmark("PositionFlag"); 12. //在 PositionFlag 书签对应的位置,插入另一个文档的内容。 13. //InsertDocument 方法可以在 /docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document 找到 14. InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);9.创建 word 模版,使用 MergeFeild 插入图片
10.插入图片示例1. 2. 3. 4. 5. 6. 7. 8. 9.string tempPath = Server.MapPath("~/Docs/Temp/Template.doc"); string logoPath = Server.MapPath("~/Content/logo.jpg"); var doc = new Document(tempPath); //载入模板 //提供数据源 String[] fieldNames = new String[] { "logo", "Gender", "BirthDay", "Address","Logo" }; Object[] fieldValues = new Object[] { "张三", "男", "1988-09-02", "陕西咸阳",logoPath }; //增加处理图片大小程序 //doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument(); //合并模版,相当于页面的渲染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.File(docStream.ToArray(), "application/msword", "Template.doc");效果如下:增加图片大小处理的程序
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.//Aspose.Word 提供了一个类似 Handler 的功能, IFieldMergingCallback 允许我们动态的处理 MergeField void IFieldMergingCallback.FieldMerging(FieldMergingArgs e){} //处理文本 void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args){} //处理图片 //这里我们处理图片写了一个自定义的类实现 class HandleMergeFieldInsertDocument : IFieldMergingCallback { //文本处理在这里,如果写在这一块,则不起作用 void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) {} //图片处理在这里 void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) { if (args.DocumentFieldName.Equals("Logo")) { // 使用 DocumentBuilder 处理图片的大小 DocumentBuilder builder = new DocumentBuilder(args.Document); builder.MoveToMergeField(args.FieldName);Shape shape = builder.InsertImage(args.FieldValue.ToString());// 设置 x,y 坐标和高宽. shape.Left = 0; shape.Top = 0; shape.Width = 60; shape.Height = 80; } }30. }
效果如下:11.向模版插入 Html这里的家乡简介使用 html 格式12.插入 html 示例1. 2. 3. 4. 5. 6. 7. 8. 9.string tempPath = Server.MapPath("~/Docs/Temp/Template.doc"); string descHtml = "";//这里是 html 文本,由于太长略去 var doc = new Document(tempPath); //载入模板 //提供数据源 String[] fieldNames = new String[] { "UserName", "Gender", "BirthDay", "Address","Desc"}; Object[] fieldValues = new Object[] { "张三", "男", "1988-09-02", "陕西咸阳", descHtml}; //增加处理 html 程序 doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml(); //合并模版,相当于页面的渲染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/msword", "Template.doc"); 15. //如果不增加 html 的处理程序,默认以文本的输出,这里我们写一个自定义的处理类
16. class HandleMergeFieldInsertHtml : IFieldMergingCallback 17. { 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. } 35. //IFieldMergingCallback 在循环结构中同样适用 36. //小结:利用书签加上标志位,利用自定义的 IFieldMergingCallback 灵活处理各种需求,后边会继续 尝试根据条件加载不同的模版 } } //图片处理在这里 void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args) { } //文本处理在这里 void IFieldMergingCallback.FieldMerging(FieldMergingArgs e) { if (e.DocumentFieldName.Equals("Desc")) { // 使用 DocumentBuilder 处理图片的大小 DocumentBuilder builder = new DocumentBuilder(e.Document); builder.MoveToMergeField(e.FieldName); builder.InsertHtml(e.FieldValue.ToString());