Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
string abc = GridView1.Rows[i].Cells[k].Text;
abc.Trim();
if (abc == " ")
{
abc = "";
}
if (abc == "")
{
}
sb.Append(abc + ',');
}
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();