Code for .cs file
protected void btnExportExcel_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); //Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); //Apply style to Individual Cells GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green"); GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green"); for (int i = 0; i < GridView1.Rows.Count;i++ ) { GridViewRow row = GridView1.Rows[i]; //Change Color back to white row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row if (i % 2 != 0) { row.Cells[0].Style.Add("background-color", "#C2D69B"); row.Cells[1].Style.Add("background-color", "#C2D69B"); row.Cells[2].Style.Add("background-color", "#C2D69B"); row.Cells[3].Style.Add("background-color", "#C2D69B"); } } GridView1.RenderControl(hw); //style to format numbers to string string style = @""; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); }
To avoid the error you will need to add this event which ensures that the GridView is Rendered before exporting.
public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }
No comments:
Post a Comment