private void ExportExcel() { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); //To Export all pages GridView1.AllowPaging = false; this.bindGridview1(); GridView1.HeaderRow.BackColor = Color.White; foreach (TableCell cell in GridView1.HeaderRow.Cells) { cell.BackColor = GridView1.HeaderStyle.BackColor; } foreach (GridViewRow row in GridView1.Rows) { row.BackColor = Color.White; foreach (TableCell cell in row.Cells) { if (row.RowIndex % 2 == 0) { cell.BackColor = GridView1.AlternatingRowStyle.BackColor; } else { cell.BackColor = GridView1.RowStyle.BackColor; } cell.CssClass = "textmode"; } } GridView1.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { mso-number-format:\@; } </style>"; HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Output.Write(sw.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.SuppressContent = true; HttpContext.Current.ApplicationInstance.CompleteRequest(); } }
protected void btnDel_Click(object sender, EventArgs e) { try { PanelPopup2.Visible = true; while (PanelPopup2.Visible == true) ExportExcel(); } catch (Exception ex) { throw new Exception(ex.Message); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)