TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Export Grid Data To Excel
Apurba Ranjan
Jul 18
2016
Code
1.1
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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