zoukankan      html  css  js  c++  java
  • Aspose Word模板使用总结

    Aspose Word模板使用总结

    命名空间:

    1. using Aspose.Words;
    2. using Aspose.Words.Saving;
    3. using System.IO;
    4. using System.Data;

    添加dll

    链接:http://pan.baidu.com/s/1pJG899T 密码:bv3k


    1.创建word模版,使用MergeFeild绑定数据

        新建一个Word文档,命名为Template.doc

     

        注意:这里并不是输入"《”和“》”就可以了,而是必须在菜单的"插入→文档部件→域”找到MergeField并输入相应的域名

     

    2.使用数组提供数据源

    1. string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");
    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(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. //创建名称为UserList的DataTable
    2. DataTable table=new DataTable("UserList");
    3. table.Columns.Add("UserName");
    4. table.Columns.Add("Gender");
    5. table.Columns.Add("BirthDay");
    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. return 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. table.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 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. var doc = new Document(tempPath);
    3. var doc1 = new Document(tempPath1);//新文档
    4. //找到名称为PositionFlag的书签
    5. var bookmark= doc.Range.Bookmarks["PositionFlag"];
    6. //清空书签的文本
    7. bookmark.Text = "";
    8. //使用DocumentBuilder对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个段落,插入空白页,追加或另一个word文件的内容等。
    9. var builder = new DocumentBuilder(doc);
    10. //定位到指定位置进行插入操作
    11. builder.MoveToBookmark("PositionFlag");
    12. //在PositionFlag书签对应的位置,插入另一个文档的内容。
    13. //InsertDocument方法可以在http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document找到
    14. InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);

    9.创建word模版,使用MergeFeild插入图片

     

     

    10.插入图片示例

    1. string tempPath = Server.MapPath("~/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.MailMerge.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.File(docStream.ToArray(), "application/msword", "Template.doc");

      效果如下:

      

     增加图片大小处理的程序

    1. //Aspose.Word提供了一个类似Handler的功能,IFieldMergingCallback允许我们动态的处理MergeField
    2. void IFieldMergingCallback.FieldMerging(FieldMergingArgs e){} //处理文本
    3. void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args){} //处理图片
    4. //这里我们处理图片写了一个自定义的类实现
    5. class HandleMergeFieldInsertDocument : IFieldMergingCallback
    6. {
    7.     //文本处理在这里,如果写在这一块,则不起作用
    8.     void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    9.     {
    10.  
    11.     }
    12.     //图片处理在这里
    13.     void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    14.     {
    15.         if (args.DocumentFieldName.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.Width = 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", "Address","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/msword", "Template.doc");
    15. //如果不增加html的处理程序,默认以文本的输出,这里我们写一个自定义的处理类
    16. class HandleMergeFieldInsertHtml : IFieldMergingCallback
    17. {
    18.     //文本处理在这里
    19.     void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    20.     {
    21.         if (e.DocumentFieldName.Equals("Desc"))
    22.         {
    23.             // 使用DocumentBuilder处理图片的大小
    24.             DocumentBuilder builder = new DocumentBuilder(e.Document);
    25.             builder.MoveToMergeField(e.FieldName);
    26.             builder.InsertHtml(e.FieldValue.ToString());
    27.         }
    28.     }
    29.     //图片处理在这里
    30.     void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    31.     {
    32.  
    33.     }
    34. }
    35. //IFieldMergingCallback在循环结构中同样适用
    36. //小结:利用书签加上标志位,利用自定义的IFieldMergingCallback灵活处理各种需求,后边会继续尝试根据条件加载不同的模版
  • 相关阅读:
    C#基础知识汇总(不断更新中)
    比较两个DataTable是否相等
    C#利用SerialPort控件进行串口编程小记
    C# ListBox 自动滚动到底部 方法:
    IIS配置文件的XML格式不正确 applicationHost.config崩溃 恢复解决办法
    net4log 添加自定义变量
    net4log 日志管理
    C#实现加简单的Http请求
    H5,Css小姐又作画了
    H5 ,Css实现了你的logo
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/8303854.html
Copyright © 2011-2022 走看看