protected void btnExcel_Click(object sender, EventArgs e)
        {
            DataTable dtData = (DataTable)ViewState["ExeclOutput"];
            string [] headinfo =(string []) ViewState["headinfo"];

            System.Web.UI.WebControls.DataGrid dgExport = null;
            // 当前对话
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            // IO用于导出并返回excel文件
            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;

            Response.Write("<p>开票通知范围指定:&nbsp;&nbsp; " + headinfo[0] + "&nbsp;至 &nbsp; " + headinfo[1] + " &nbsp;&nbsp;&nbsp;&nbsp;   客户方:&nbsp;&nbsp;  " + headinfo[2] + " &nbsp;&nbsp;&nbsp;&nbsp;   销售合同编号:&nbsp;&nbsp;  " + headinfo[3] + "</p>");

            if (dtData != null)
            {
              
                // 设置编码和附件格式
                string style = @"<style> .text { mso-number-format:\@; } </script> "; //设置格式,解决电话不显示开头是0的问题
                string FileName = "";
                FileName = "InvoiceNoticeManage" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";
                curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
                curContext.Response.Charset = "UTF-8";
                curContext.Response.ContentEncoding = System.Text.Encoding.Default;
                curContext.Response.ContentType = "application/ms-excel";

                // 导出excel文件
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

                // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
                dgExport = new System.Web.UI.WebControls.DataGrid();
                dgExport.DataSource = dtData.DefaultView;
                dgExport.AllowPaging = false;
                dgExport.DataBind();

                // 返回客户端
                dgExport.RenderControl(htmlWriter);
                curContext.Response.Write(style);//调用格式化字符串
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
        }

如果是在UpdatePanel中的话必须要在Triggers添加按钮名称

 <Triggers>
            <asp:PostBackTrigger ControlID="btnExcel" />
  </Triggers>