利用Aspose.Words打印word文档
来源:互联网 发布:淘宝人像摄影教程 编辑:程序博客网 时间:2024/04/30 07:30
Aspose.Words是一款先进的类库,可以直接在各个应用程序中执行各种文档处理,即使在没有Microsoft word
的情况下,仍然可以生成,更改,转换,渲染和打印word文档,并且支持DOC,PDF等其他格式。
下面就以 Aspose.Words如何实现打印word文档为例做一个小demo。
1·制作word模板
在word模板中,在需要插入的地方选择插入--文档部件--域--域名是MergeField--域名是字段名,例如name,点击确定即可。
效果图如下:
2 实现代码:
由于我是加入MVC框架的,所以在controller中接收的是来自view的实体,不用再经过数据库查找相应的数据,如果在纯三层或几层中实现,直接调用D层,通过D层访问数据库获得即可。
简单一句话来说,就是一个赋参数的一个过程,然后直接调用该类库下的方法即可。方法有很多,具体的可以参考Aspose.Word的API文档。
#region 初婚未育调查报告 + void FirstMarriageReport(FamilyPlanInfoViewModel enFamilyInfo) 贾丽敏 2015/9/3 /// <summary> /// 初婚未育调查报告 /// </summary> /// <param name="enFamilyInfo">FamilyPlanInfoViewModel</param> public void FirstMarriageReport(FamilyPlanInfoViewModel enFamilyInfo) { if (Session["userID"] != null)//判断登陆时用户ID是否为空,为空则跳转登陆页面 { //获取模板的路径 var path = Server.MapPath("../Document/2初婚未育调查报告.doc"); #region 初婚未育调查报告中各参数赋值 string strUserID = Session["userID"].ToString(); while (strUserID.Length < 4) { strUserID = "0" + strUserID; } //获取信函编号 string strCardNo = numberInfo.GetNumber(); while (strCardNo.Length < 4) { strCardNo = "0" + strCardNo; } //获取documentID参数 string strDocumentID = "LFRC" + strUserID + "No." + strCardNo; //赋值 string name = enFamilyInfo.name;//姓名 string sex = enFamilyInfo.sex;//性别 string spouseWorkplace = enFamilyInfo.spouseworkplace;//配偶工作地 string spousename = enFamilyInfo.spousename;//配偶姓名 string year, month, date; if (enFamilyInfo.idNumber == null) { year = ""; month = ""; date = ""; } else { year = enFamilyInfo.idNumber.Substring(6, 4);//出生年份 month = enFamilyInfo.idNumber.Substring(10, 2);//出生月份 date = enFamilyInfo.idNumber.Substring(12, 2);//出生日 } string marryyear, marrymonth, marrydate; if (enFamilyInfo.marriageregistrationdate1 == null) { marryyear = "";//结婚登记年份 marrymonth = " ";//结婚登记月份 marrydate = " ";//结婚登记日 } else { marryyear = enFamilyInfo.marriageregistrationdate1.Substring(0, 4); marrymonth = enFamilyInfo.marriageregistrationdate1.Substring(5, 1); marrydate = enFamilyInfo.marriageregistrationdate1.Substring(7, 2); } #endregion string fileName = "初婚未育调查报告.doc"; //获取模板中设定好的字段 String[] fieldNames = new String[] { "documentID", "name", "sex", "year", "month", "date", "marryyear", "marrymonth", "marrydate", "spouseworkplace", "spousename" }; //给模版中参数赋值 Object[] fieldValues = new Object[] { strDocumentID, name, sex, year, month, date, marryyear, marrymonth, marrydate, spouseWorkplace, spousename }; Print(path, fileName, fieldNames, fieldValues);//调用封装好的print方法,在下方。 } else { //跳转到登陆界面 this.Response.Write("<script type='text/javascript'> goIndex(); function goIndex(){window.location.href='../Login/Login';}</script>"); } } #endregion
封装好的print方法:
#region 封装信函打印 + void Print(string path, String[] fieldNames, Object[] fieldValues) 贾丽敏 2015/9/3 /// <summary> /// 封装信函打印 /// </summary> /// <param name="path">文件路径</param> /// <param name="fieldNames">参数数组</param> /// <param name="fieldValues">参数值数组</param> public void Print(string path, string fileName, String[] fieldNames, Object[] fieldValues) { //string fileName = path.Substring(path.LastIndexOf("\\") + 1);//获取文件名 var ouputPath = Server.MapPath("../Document/"); Document doc = new Document(path); //合并模版,相当于页面的渲染 doc.MailMerge.Execute(fieldNames, fieldValues); ouputPath = ouputPath + fileName; //保存合并后的文档,生成的文档存放到服务器硬盘上 doc.Save(ouputPath); //通过response对象,执行下载操作 System.IO.FileInfo file = new System.IO.FileInfo(ouputPath); if (file.Exists)//判断文件是否存在 { Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.AddHeader("content-disposition", "attachment;filename=" + file.Name); Response.AddHeader("cintent_length", "attachment;filename=" + HttpUtility.UrlDecode(file.Name)); Response.AddHeader("cintent_length", file.Length.ToString()); //设定输入文件的类型 Response.ContentType = "application.vnd.ms-word"; Response.WriteFile(file.FullName); Response.Flush(); Response.End(); //从服务器硬盘中删除刚生成的Word文件 System.IO.File.Delete(ouputPath); } } #endregion最后效果图如下:
实现word打印的方式不仅仅是这一种,这只是其中的一个demo而已,最好的了解方式就是Aspose.Words的api文档。而且这一点需求在实际业务用处很大。
0 0
- 利用Aspose.Words打印word文档
- 利用Aspose.Words,在word文档中创建表格
- 无需安装Microsoft Words即可读写打印Word文档的控件Aspose.Words
- Aspose.Words在Word文档应用
- word,txt转PDF 利用Aspose.Words
- 利用Aspose.Words 把word 生成图片
- 利用Aspose.Words按模板导出Word
- Aspose.Words 替换word值和保存打印
- 用Aspose.Words 从Word文档中提取表格数据
- 使用Aspose.Words设置word文档多倍行距
- 用Aspose.Words 从Word文档中提取表格数据
- 使用Aspose.BarCode 和 Aspose.Words从Word文档中识别条码
- 利用Aspose.Word控件实现Word文档的操作
- 利用Aspose.Word控件实现Word文档的操作
- 利用Aspose.Word for .NET实现Word文档的操作
- 利用Aspose.Word控件实现Word文档的操作
- 利用Aspose.Word控件实现Word文档的操作
- 利用Aspose.Word控件实现Word文档的操作
- hdu5433 Xiao Ming climbing(BestCoder Round #55 ($) )
- iOS正则表达式使用
- 签证之家
- php向数据库中插入中文乱码问题
- HDU 5439 Aggregated Counting
- 利用Aspose.Words打印word文档
- Opencv+Kinect2.0获取景深图
- POJ 1401&&NYOJ 954 N!
- hdu 5438 Ponds(并查集+拓扑序)
- Linux 线程同步的三种方法
- 软件测试中的白盒测试
- Linux chmod命令详解 http://www.cnblogs.com/younes/archive/2009/11/20/1607174.html
- 架构师速成-架构目标之可用性
- Paper阅读笔记_1