Aspose Word模板使用总结

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(bookmark.BookmarkStart.ParentNode, doc1);

Aspose Word模板使用总结的更多相关文章

  1. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  2. Csharp: Create Excel Workbook or word from a Template File using aspose.Word 14.5 and aspose.Cell 8.1

    winform: /// <summary> /// /// </summary> /// <param name="sender"></ ...

  3. 利用Aspose.Word控件实现Word文档的操作

    Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...

  4. 黄聪:利用Aspose.Word控件实现Word文档的操作(转)

    撰写人:伍华聪  http://www.iqidi.com  Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...

  5. aspose.word使用简单方法

    概念介绍 使用aspose生成word报表步骤: 加载word模板 提供数据源 填充 加载模板 提供了4种重载方法 public Document(); public Document(Stream ...

  6. aspose.word 使用简单方法

    aspose.word使用简单方法 概念介绍 使用aspose生成word报表步骤: 加载word模板 提供数据源 填充 加载模板 提供了4种重载方法 1 2 3 4 5 public Documen ...

  7. C# 操作word 模板 值 替换

    1.引用 aspose.words   dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...

  8. JAVA Asponse.Word Office 操作神器,借助 word 模板生成 word 文档,并转化为 pdf,png 等多种格式的文件

    一,由于该 jar 包不是免费的, maven 仓库一般不会有,需要我们去官网下载并安装到本地 maven 仓库 1,用地址   https://www-evget-com/product/564  ...

  9. 使用Aspose.word (Java) 填充word文档数据(包含图片填充)

    Aspose填充word数据 本文介绍了如何使用aspose进行word文档的生成,并提供了工具类供参考. 有问题欢迎 call 微信:905369866,小弟尽力而为..毕竟这玩意没吃透. 目录 A ...

随机推荐

  1. SQL2008游标

    最近让写一个自动生成数据的存储过程,其中会遍历表中数据并做出相应处理,因为数据量不算太大所以使用到了游标,初识游标遇到几个小问题,所以来和大家一起分享一下: 使用游标的五个步骤: 1.声明游标 语法: ...

  2. 关于Java项目打包

    可以选择以下几种办法: 一.使用Eclipse,右键项目导出jar. 二.使用Eclipse,右键项目导出runnable jar. 三.使用Eclipse 插件fat jar,导出可执行的jar包. ...

  3. Claims Identity

    using System;using System.Collections.Generic;using System.Linq;using System.Security.Claims;using S ...

  4. Java处理excel文件

    好久好久没写blog了,感觉都生锈了,最近弄了弄java处理excel,特来简单粘贴一下: package excel; import java.io.BufferedInputStream; imp ...

  5. c#简易反射调用泛型方法

    // 所谓程序集的简单理解,存在不同项目中(不是解决方案),即using前需要引用**.dll 1.调用当前类文件下的方法public List<T> GetByCondition< ...

  6. putty自动登录

    如果没有公钥/密钥对,就用 PuTTYgen 创建一个,已经有了就可以忽略这一步.一个公钥/密钥对可以用在不同的服务器上,所以也不需要重复创建,关键要有足够强健的密码和安全的存放. 象先前一样输入帐户 ...

  7. 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 适者:survival of the fittest 适者:survival of the fittest

    编程什么的最讨厌了,总是忘记一些乱七八糟的,看起来并没有什么乱用的,比如(::“<>{}, 还有交作业的时候总是忽略大小写<(▰˘◡˘▰)> 马马虎虎莫名其妙就错了,其实大小写 ...

  8. 怎么改变 placeholder字体颜色

    ::-webkit-input-placeholder{color: #888}::-moz-placeholder{color: #888}:-moz-placeholder{color: #888 ...

  9. 使用WIC组件转换图片格式

    #include <windows.h>#include <Wincodec.h>#pragma comment(lib, "Windowscodecs.lib&qu ...

  10. AJAX浏览器判断

    第一步要先获取对象: var xmlHttp; 第二是判断浏览器 function getXmlHttp(){ if(window.ActiveXObject){ xmlHttp = new Acti ...