Monday, March 5, 2012

Export to Excel button click code n asp.net Web Applications

 protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Clear();
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "DealerReport.xls"));
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                gdvDealers.AllowPaging = false;
                gdvDealers.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
            }
            catch (Exception ex)
            {
                CallJS("jAlert('" + ex.Message + "','Error Message',this.focus());");
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

No comments:

Post a Comment