protected void btnExcel_Click(object sender, EventArgs e) { string name = ""; string code = ""; if (Session["ManufacturerList-Name"] != null) { name = Session["ManufacturerList-Name"].ToString(); } if (Session["ManufacturerList-Code"] != null) { code = Session["ManufacturerList-Code"].ToString(); } int maximumRows = int.MaxValue; int startRowIndex = 0; List <Manufacturer> list = Manufacturer.GetPagedManufacturerList(name, code, ref maximumRows, ref startRowIndex); gvExport.DataSource = list; gvExport.DataBind(); gvExport.Visible = true; if (list.Count >= 1) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=ManufacturerList.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; } }