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:
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(8)
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//append a separator
sb.Append(GridView1.Columns[k].HeaderText +» ver comentario
Good sample.
Helped a lot.
Hearty» ver comentario
Excellent code. However when exporting nulls are converted to   and carriage returns to tab-delimiters. Any thoughts on how to change these two» ver comentario
Ankit, try to put the value of the field between " (double quotes). You force the field to be string» ver comentario
Your code as been of great help to me...but I want leading zeros not to be terminated when exported to .csv....Can you help me in this regard? Thanks» ver comentario

















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