In this article I will explain how to export ASP.Net GridView control with Caption or Title to Excel file.
GridView by default has a caption property but the problem is that it does not have much option of styling and alignment. Hence I have developed a trick where I am making use of Panel control and then exporting the Panel control to Excel file
 
HTML Markup
Below is the HTML Markup of the page. I have placed the GridView control with a Label which will be the Caption or Title inside an ASP.Net Panel control. Also there’s a Button control that will trigger the export process.
<asp:Panel runat="server" ID="Panel1">
    <table style="width: 210px">
        <tr>
            <td style="background-color: green; border: 1px solid black" align="center">
                <asp:Label ID="lblCaption" runat="server" Text="Store Item Details" Style="font-weight: bold;
                    color: White;"></asp:Label>
            </td>
        </tr>
    </table>
    <br />
    <br />
    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
        RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
        runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="100px" />
            <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-Width="100px" />
        </Columns>
    </asp:GridView>
</asp:Panel>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="Export" />
 
 
Namespaces
C#
using System.IO;
using System.Data;
 
VB.Net
Imports System.IO
Imports System.Data
 
 
Populating the GridView with data
Below is the code to populate the ASP.Net GridView control. I am populating a DataTable with some sample data and then binding it to the GridView in the Page Load event of the page.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
        dt.Rows.Add("Shirt", 199);
        dt.Rows.Add("Football", 020);
        dt.Rows.Add("Shirt", 566);
        dt.Rows.Add("Disc", 099);
        dt.Rows.Add("Watch", 54);
        dt.Rows.Add("Clock", 890);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Item"), New DataColumn("Price")})
        dt.Rows.Add("Shirt", 199)
        dt.Rows.Add("Football", 20)
        dt.Rows.Add("Shirt", 566)
        dt.Rows.Add("Disc", 99)
        dt.Rows.Add("Watch", 54)
        dt.Rows.Add("Clock", 890)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
Export ASP.Net GridView with Caption ( Title ) to Excel file
 
 
Exporting the Panel with GridView and Label to Excel
Below is the code to export the Panel with the GridView and the Label to Excel fie.
C#
protected void Export(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Export.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        Panel1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}
 
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}
 
VB.Net
Protected Sub Export(sender As Object, e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=Export.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Using sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        Panel1.RenderControl(hw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
    End Using
End Sub
 
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered
End Sub
 
Export ASP.Net GridView with Caption ( Title ) to Excel file
 
Demo
 
Downloads