Aspose Word模版使用总结

来源:互联网 发布:天天练软件好吗 编辑:程序博客网 时间:2024/06/05 04:36
1.创建word模版,使用MergeFeild绑定数据
    新建一个Word文档,命名为Template.doc
    注意:这里并不是输入"《”和“》”就可以了,而是必须在菜单的"插入→文档部件→域”找到MergeField并输入相应的域名
2.使用数组提供数据源
 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);
 //保存合并后的文档
 doc.Save(outputPath);
  //在WebForm中,保存文档到流中,使用Response. BinaryWrite输出该文件
  var docStream = new MemoryStream();
  doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  Response.ContentType = "application/msword";
  Response.AddHeader("content-disposition", "attachment;  filename=Template.doc");
  Response.BinaryWrite(docStream.ToArray());
  Response.End();
 //在MVC中采用,保存文档到流中,使用base.File输出该文件
  var docStream = new MemoryStream();
  doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  return base.File(docStream.ToArray(), "application/msword","Template.doc");
3.创建循环数据的模版,这里的循环数据类似页面的for结构,不拘泥于形式table

   «TableStart:UserList»

   姓名:«UserName»

   «TableEnd:UserList»

   

4.使用DataTable提供数据源

//创建名称为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);
 //提供数据源
 var datatable= GetDataTable();
 //合并模版,相当于页面的渲染
 doc.MailMerge.ExecuteWithRegions(datatable);
 var docStream = new MemoryStream();
 doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
 return base.File(docStream.ToArray(), "application/msword","Template.doc");
 
 5.绑定带有子循环数据模版
6.使用DataSet提供数据源
//用户表结构
 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");
 table.Columns.Add(new DataColumn("UserId", typeof(int)));
 table.Columns.Add("Name");
 table.Columns.Add("Score");
//----------------------------------------------------------------------------------------------------
//载入模板
 var doc = new Document(tempPath);
 //提供数据源
 DataSet dataSet = new DataSet();
 var userTable= GetUserDataTable();
 var userScoreTable= GetUserScoreDataTable();
 dataSet.Tables.Add(userTable);
 dataSet.Tables.Add(userScoreTable);
 dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"], userScoreTable.Columns["UserId"]));
 //合并模版,相当于页面的渲染
 doc.MailMerge.ExecuteWithRegions(dataSet);
 var docStream = new MemoryStream();
 doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
 return base.File(docStream.ToArray(), "application/msword","Template.doc");
7.模版上使用书签,插入标记位置
选中文档中的文字,在菜单的"插入→书签”指定书签的名称,排序依据选定为位置,添加一个新书签。选中的文字为书签的Text属性,这里是为了方便查看。也可以直接插入一个书签并指定位置,只是不明显。
8.在书签位置插入另一个文档的内容
//载入模板
 var doc = new Document(tempPath);
 var doc1 = new Document(tempPath1);//新文档
//找到名称为PositionFlag的书签
 var bookmark= doc.Range.Bookmarks["PositionFlag"];
//清空书签的文本
 bookmark.Text = "";
//使用DocumentBuilder对象插入一些文档对象,如插入书签,插入文本框,插入复选框,插入一个段落,插入空白页,追加或另一个word文件的内容等。
 var builder = new DocumentBuilder(doc);
//定位到指定位置进行插入操作
 builder.MoveToBookmark("PositionFlag");
//在PositionFlag书签对应的位置,插入另一个文档的内容。
//InsertDocument方法可以在http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document找到

 InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);

9.创建word模版,使用MergeFeild插入图片
10.插入图片示例
  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();
   //合并模版,相当于页面的渲染
    doc.MailMerge.Execute(fieldNames, fieldValues);
 
   //在MVC中采用,保存文档到流中,使用base.File输出该文件
   var docStream = new MemoryStream();
   doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
   return base.File(docStream.ToArray(), "application/msword", "Template.doc");
  效果如下:
  
 增加图片大小处理的程序
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;
            }
        }
    }
效果如下:
11.向模版插入Html
这里的家乡简介使用html格式
12.插入html示例
 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();
 //合并模版,相当于页面的渲染
 doc.MailMerge.Execute(fieldNames, fieldValues);
 //在MVC中采用,保存文档到流中,使用base.File输出该文件
 var docStream = new MemoryStream();
  doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  return base.File(docStream.ToArray(), "application/msword", "Template.doc");
如果不增加html的处理程序,默认以文本的输出,这里我们写一个自定义的处理类
 class HandleMergeFieldInsertHtml : IFieldMergingCallback
    {
        //文本处理在这里
        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());
            }
        }
        //图片处理在这里
        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        {
           
        }
    }

0 0
原创粉丝点击