博 之 文

以 拼 搏 设 计 梦 想 , 以 恒 心 编 程 明 天
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

下载Excel模板后台代码

Posted on 2012-03-04 17:48  IsNull_Soft  阅读(283)  评论(0)    收藏  举报

///下载Excel模板

///注:本例在微软无刷新UppdatePanel控件里不能使用,因为本例在UppdatePanel控件里没有回传信息,所以服务器未能收到回传信息导致无法运行;

protected void DownLoad_Click(object sender, EventArgs e)
        {
            FileInfo file = new System.IO.FileInfo(Server.MapPath("~/Template/Employee_r.xls"));
            try
            {
                System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            }
            catch (Exception ex)
            {
                Comment.Alert.AlertAndBack("没有找到附件路径,或附件已被删除!");
                return;
            }

            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("客户资料模版", System.Text.Encoding.UTF8) + ".xls");
            System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            System.Web.HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(Server.MapPath("~/Template/Employee_r.xls")));
            System.Web.HttpContext.Current.Response.Flush();
            System.Web.HttpContext.Current.Response.End();
        }