Protected void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grvUsers.AllowPaging = false;
grvUsers.DataBind();
//Change the Header Row back to white color
grvUsers.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
//grvUsers.HeaderRow.Cells[0].Style.Add("background-color", "Green");
grvUsers.HeaderRow.Cells[0].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[1].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[2].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[3].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[4].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[5].Style.Add("background-color", "#EF6E16");
grvUsers.HeaderRow.Cells[6].Style.Add("background-color", "#EF6E16");
for (int i = 0; i < grvUsers.Rows.Count; i++)
{
GridViewRow row = grvUsers.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
//row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[0].Style.Add("background-color", "#62BDBB");
row.Cells[1].Style.Add("background-color", "#62BDBB");
row.Cells[2].Style.Add("background-color", "#62BDBB");
row.Cells[3].Style.Add("background-color", "#62BDBB");
row.Cells[4].Style.Add("background-color", "#62BDBB");
row.Cells[5].Style.Add("background-color", "#62BDBB");
row.Cells[6].Style.Add("background-color", "#62BDBB");
}
}
grvUsers.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
No comments:
Post a Comment