News and events
- Popups with FancyBox Hello... today I'm gonna teach you how to show a popup with FancyBox. FancyBox is a tool to show images, html content and...
- Booking Calendar - Online booking plugin for Wordpress Browsing the web, I found this plugin to make online bookings. It's a plugin that allow us to add a form into our blog to make...
- Export a GridView to Word format with ASP.net Following our series of Export to... this time is the turn of Word format. (.doc). Still being really simple. We put the next...
Contact us
If you have any doubt or suggestion, you may contact us with the next form:
Export a GridView to Excel format with ASP.net
created by Ernesto Traversaro - 01/06/2009
With the next code you will be able to export a GridView to Excel format (xls), it's very simple:
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();
}
Good bye

















![Validate my RSS feed [Valid RSS]](/img/valid-rss.png)
