您的位置:首页 > 编程语言 > ASP

asp.net 生成 excel 解决迅雷下载

2015-01-27 10:20 441 查看
看到论坛里面遇到迅雷下载问题。有很多帖子废话连天,让迅雷见鬼去吧。今天贴出源码供大家查看。

protected void Button1_Click(object sender, EventArgs e)

{

string excelFile = Server.MapPath("~/UploadFile/123456.xls");//文件服务器地址

string excelName = "123456.xls";//客户端保存文件名称和类型

FileInfo fi = new FileInfo(excelFile);//excelFile为文件在服务器上的地址

HttpResponse contextResponse = HttpContext.Current.Response;

contextResponse.Redirect(string.Format("~/UploadFile/{0}", excelName), false);

contextResponse.Clear();

contextResponse.Buffer = true;

contextResponse.Charset = "GB2312"; //设置了类型为中文防止乱码的出现

contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", excelName)); //定义输出文件和文件名

contextResponse.AppendHeader("Content-Length", fi.Length.ToString());

contextResponse.ContentEncoding = Encoding.Default;

contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

contextResponse.WriteFile(fi.FullName);

contextResponse.Flush();

contextResponse.End();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐