in my gridview there is no color in header row but when i am doing expert to excel i want my excel has header color like yellow or somthing else. I have tried all the comment lines in bellow code to do this but still it is not working.Could you help me?
private void ExportGridToExcel(string strFileName, GridView lGridview)
{
try
{
string Header = string.Empty;
if (lblmsg.Text != "")
Header = lblmsg.Text;
else
Header = "Unitwise Hotel Report";
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = strFileName.Replace(",", " ").Replace(":", " ").Replace("/", " ") + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
//grdUnitwiseHtl.HeaderRow.Style.Add("background-color", "Blue");
//grdUnitwiseHtl.HeaderRow.Style.Add("bgColor", "FF8080");
//grdUnitwiseHtl.HeaderRow.CssClass = "header";
//grdUnitwiseHtl.HeaderStyle.BackColor = System.Drawing.Color.DarkBlue;
//grdUnitwiseHtl.BackColor = System.Drawing.Color.Red;
//System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(strwritter);
////Add the control to the forms
//DataGrid dgRecord = new DataGrid();
//this.grdUnitwiseHtl.Controls.Add(dgRecord);
////Color Setttings
//grdUnitwiseHtl.HeaderStyle.BackColor = System.Drawing.Color.Blue;
////Cells color settings
//foreach (DataGridItem dgi in dgRecord.Items)
// foreach (TableCell tcGridCells in dgi.Cells)
// tcGridCells.Attributes.Add("class", "sborder");
////Render the datagrid
//dgRecord.RenderControl(htmlWrite);
//for (int col = 0; col < grdUnitwiseHtl.HeaderRow.Controls.Count; col++)
//{
// TableCell tc = grdUnitwiseHtl.HeaderRow.Cells[col];
// tc.Style.Add("color", "#FFFFFF");
// tc.Style.Add("background-color", "#444");
//}
grdUnitwiseHtl.GridLines = GridLines.Both;
grdUnitwiseHtl.HeaderStyle.Font.Bold = true;
grdUnitwiseHtl.HeaderStyle.ForeColor = System.Drawing.Color.Yellow;
grdUnitwiseHtl.HeaderStyle.Wrap = true;
grdUnitwiseHtl.RowStyle.Wrap = true;
grdUnitwiseHtl.RenderControl(htmltextwrtter);
//string s = htmltextwrtter.InnerWriter.ToString();
//if (s.Contains("<tr class"))
//{
// s = s.Replace("<tr ", "<tr style='font-weight:bold;' bgColor='#FF8080'");
//}
//htmltextwrtter.InnerWriter.Write(s);
string headerTable = @"<Table bgColor='#FF8080' ><tr><td colspan='3' rowspan='2' ><B><span style='text-align:center'>" + strFileName + "</span></B></td></tr></Table>";
Response.Write(headerTable);
Response.Write(strwritter.ToString());
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}