protected void btnExcel_Click(object sender, EventArgs e) { string fullname = ""; int scheduledVaccinationId = -1; if (Session["DoseList-Fullname"] != null) { fullname = Session["DoseList-Fullname"].ToString(); } if (Session["DoseList-ScheduledVaccinationId"] != null) { scheduledVaccinationId = int.Parse(Session["DoseList-ScheduledVaccinationId"].ToString()); } int maximumRows = int.MaxValue; int startRowIndex = 0; List <Dose> list = Dose.GetPagedDoseList(fullname, scheduledVaccinationId, ref maximumRows, ref startRowIndex); gvExport.DataSource = list; gvExport.DataBind(); gvExport.Visible = true; if (list.Count >= 1) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=DoseList.xls"); Response.Charset = ""; // If you want the option to open the Excel file without saving then // comment out the line below // Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/ms-excel"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); gvExport.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); gvExport.Visible = false; } }