欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-10-27 09:14:34
第一种 通过网页直接生成word下载 response.addheader("content-disposition", "attachment;filename...
第一种 通过网页直接生成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(p) 的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