c# asp.net 下载excel到本地的方法分享


摘要:
下面的代码可实现下载excel文件至客户端任意位置,如下:
实验环境:iis

例:
下载服务器端tmp目录下的指定excel文件至客户端

   try
{
    string strServerPath  = Server.MapPath("~") + "/tmp/20190823001.xlsx";//待下载服务器文件路径
    FileInfo  f= new FileInfo(strServerPath);
    Response.Clear();
    Response.Charset = "GB2312";
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(f.Name));
    Response.AddHeader("Content-Length", f.Length.ToString());
    Response.ContentType = "application/x-bittorrent";
    Response.WriteFile(f.FullName);
    Response.End();
}
catch (Exception ex)
{
    //异常处理
}