Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Export Gridview in CSV
WhatsApp
Ashish Srivastava
Jul 08
2016
849
0
0
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();
for
(
int
k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText +
','
);
}
//append new line
sb.Append(
"\r\n"
);
for
(
int
i = 0; i < GridView1.Rows.Count; i++)
{
for
(
int
k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text +
','
);
}
//append new line
sb.Append(
"\r\n"
);
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
Gridview CSV Export In C#
Up Next
How to Export Gridview in CSV