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

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

 


    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();
    }

猜你喜欢

转载自blog.csdn.net/suheonline/article/details/43191249
今日推荐