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
- Aspose Word模版使用总结
- Aspose Word模版使用总结篇(2)
- Aspose Word模板使用总结
- Aspose Word模板使用总结
- Aspose Word模板使用总结
- Aspose Word使用
- 导出word使用模版
- Aspose.word在asp.net mvc中如何使用的个人总结
- JAVA使用aspose.word 实现html转换word
- 使用Aspose.word生成.pdf和.doc(word)报告文件
- aspose.words for asp.net word模版-修改标签-合并单元格
- 使用Aspose.BarCode 和 Aspose.Words从Word文档中识别条码
- Asp.net使用Aspose.Words 组件生成Word
- .net 使用 Aspose.Words 进行 Word替换操作
- .net 使用 Aspose.Words 进行 Word替换操作
- Aspose.Words使用教程大全(操作Word大全帖子)
- .net 使用 Aspose.Words 进行 Word替换操作
- 使用Aspose.Words设置word文档多倍行距
- TensorFlow实战13:实现策略网络(强化学习一)
- Liunux 编程遇到的SIGBUS信号
- RN-性能优化 (二)
- springmvc入门之注解方式重点解析
- 日期选择器 很多类型的
- Aspose Word模版使用总结
- RN-性能优化 (三)
- ASP连接Mysql小记
- Spring和SpringMVC父子容器关系初窥
- 小结:区间处理和统计与高级数据结构
- 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾
- Delphi之指针使用
- 一次由于开机磁盘自检导致的启动故障排查
- RN-性能优化 (四)