首页 > Web开发 > 详细

asp.net 生成下载word的两种方式

时间:2015-11-19 16:55:18      阅读:236      评论:0      收藏:0      [点我收藏+]
第一种 通过网页直接生成word下载
Response.AddHeader("Content-Disposition", "attachment;filename=" 
    + HttpUtility.UrlEncode("word名称.doc", Encoding.UTF8));
Response.ContentType = "application/ms-word";
EnableViewState = false;
var tw = new StringWriter();
var hw = new HtmlTextWriter(tw);
tb1.RenderControl(hw);//tb1为网页table(div) 的id
Response.Write(tw.ToString());
Response.End();

第二种 word模板直接下载
string sPath = System.IO.Path.GetDirectoryName(this.Page.Request.PhysicalPath);
string a =@"\wendang\销售合同.doc";
string tmpFileName = sPath + a;//@"E:\广大连锁医药\YK_ERP_HNCC\ERPCC\Order\wendang\C++程序设计04737试题及答案2008~2012.doc";

#region
//方法一
FileInfo tmpFI = new FileInfo(tmpFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(tmpFileName), System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", tmpFI.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(tmpFileName);
Response.Flush();
Response.End();

//方法二
Response.ContentType = "Application/msword";
Response.WriteFile(tmpFileName);
Response.End();
#endregion


本文出自 “技术引领世界” 博客,谢绝转载!

asp.net 生成下载word的两种方式

原文:http://smileface.blog.51cto.com/5740492/1714649

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!