01.
protected
void
btnExcel_Click(
object
sender, ImageClickEventArgs e)
02.
{
03.
Response.ClearContent();
04.
Response.Buffer =
true
;
05.
Response.AddHeader(
"content-disposition"
,
string
.Format(
"attachment; filename={0}"
,
"Employees.xls"
));
06.
Response.Charset =
""
;
07.
Response.ContentType =
"application/vnd.ms-excel"
;
08.
StringWriter sw =
new
StringWriter();
09.
HtmlTextWriter htw =
new
HtmlTextWriter(sw);
10.
MyGridView.AllowPaging =
false
;
11.
MyGridView.DataBind();
12.
13.
MyGridView.HeaderRow.Style.Add(
"background-color"
,
"#FFFFFF"
);
14.
15.
for
(
int
i = 0; i < MyGridView.HeaderRow.Cells.Count; i++)
16.
{
17.
MyGridView.HeaderRow.Cells[i].Style.Add(
"background-color"
,
"#507CD1"
);
18.
}
19.
int
j = 1;
20.
21.
foreach
(GridViewRow gvrow
in
MyGridView.Rows)
22.
{
23.
gvrow.BackColor = Color.White;
24.
if
(j <= MyGridView.Rows.Count)
25.
{
26.
if
(j % 2 != 0)
27.
{
28.
for
(
int
k = 0; k < gvrow.Cells.Count; k++)
29.
{
30.
gvrow.Cells[k].Style.Add(
"background-color"
,
"#EFF3FB"
);
31.
}
32.
}
33.
}
34.
j++;
35.
}
36.
MyGridView.RenderControl(htw);
37.
Response.Write(sw.ToString());
38.
Response.End();
39.
}
40.
protected
void
btnWord_Click(
object
sender, ImageClickEventArgs e)
41.
{
42.
MyGridView.AllowPaging =
false
;
43.
MyGridView.DataBind();
44.
Response.ClearContent();
45.
Response.AddHeader(
"content-disposition"
,
string
.Format(
"attachment; filename={0}"
,
"Employees.doc"
));
46.
Response.Charset =
""
;
47.
Response.ContentType =
"application/ms-word"
;
48.
StringWriter sw =
new
StringWriter();
49.
HtmlTextWriter htw =
new
HtmlTextWriter(sw);
50.
MyGridView.RenderControl(htw);
51.
Response.Write(sw.ToString());
52.
Response.End();
53.
}