Hi,
If you wrap your GridView in a Panel control, then add label controls for the title and report date, something like:
<asp:panel id="pnlContainer" runat="server" xmlns:asp="#unknown">
<h4><asp:label id="lblTitle" runat="server" text="Your Report Title"></asp:label></h4>
<asp:label id="lblReportDate" runat="server"></asp:label>
<asp:gridview id="GridView1" runat="server">
<columns>
...
...
</columns>
</asp:gridview>
</asp:panel>
You can then render that Panel back to the client.
private void ExportGridToExcel()
{
lblReportDate.Text = DateTime.Now.ToString();
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "Report" + DateTime.Now + ".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);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
Panel1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
Because you are not generating a true excel file, you are limited in the format of the rendered document.
You would be
much better off have a look at a component like
EPPlus[
^] that is designed to generate excel documents, and provides lots of functionality that is available in an Excel document.
Articles explaining how to use can be found:
1)
Create/Read/Edit Advance Excel 2007/2010 Report in C#.Net using EPPlus[
^]
2)
Insert Access Format Filter Setting Formula Header[
^]
... hope it helps.