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
Export Grid Data To Excel
WhatsApp
Apurba Ranjan
Jul 18
2016
1.2
k
0
1
C# Code
protected
void
ExportToExcel(
object
sender, EventArgs e)
{
Response.Clear();
Response.Buffer =
true
;
Response.AddHeader(
"content-disposition"
,
"attachment;filename=FileName.xls"
);
Response.Charset =
""
;
Response.ContentType =
"application/vnd.ms-excel"
;
using
(StringWriter sw =
new
StringWriter())
{
HtmlTextWriter hw =
new
HtmlTextWriter(sw);
//To Export all pages
gridView1.AllowPaging =
false
;
this
.BindGrid(); //Your Method to bind GridView
gridView1
.HeaderRow.BackColor = Color.White;
foreach
(TableCell cell
in
gridView1
.HeaderRow.Cells)
{
cell.BackColor =
gridView1
.HeaderStyle.BackColor;
}
foreach
(GridViewRow row
in
gridView1
.Rows)
{
row.BackColor = Color.White;
foreach
(TableCell cell
in
row.Cells)
{
if
(row.RowIndex % 2 == 0)
{
cell.BackColor =
gridView1
.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor =
gridView1
.RowStyle.BackColor;
}
cell.CssClass =
"textmode"
;
}
}
gridView1
.RenderControl(hw);
//style to format numbers to string
string
style = @
"<style> .textmode { } </style>"
;
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
Export Grid
Exort Grid to Excel
Up Next
Export Grid Data To Excel