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 CSV with ASP.net
created by Ernesto Traversaro - 27/05/2009
This example is very easy to implement. We only need to put the next code in the desired button:
protected void btnExportCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
//iloop through the columns
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//append a separator
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
//append a new line
sb.Append("\r\n");
//loop through the rows
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//append a separator
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
//append a new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
Hope you find this helpful
Good bye
Comments(2)
Greater Code (12-09-2009)
This is very good code but how to export to excel with Unicode format? Thank in» ver comentario
Mahmoud (15-06-2009)
Dear Ernesto Traversaro ,
Actually this is a greater code and is very» ver comentario

















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