In this article I will explain with an example, how to export GridView to HTML File (HTML String) in ASP.Net using C# and VB.Net.
The GridView will be first rendered as an HTML String which will later downloaded as an HTML (.htm) file in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with three columns and a Button to export GridView to HTML file.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false" Font-Size="10" Font-Names="Arial">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="80" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="Export" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
 
VB.Net
Imports System.IO
Imports System.Data
 
 
Binding the GridView
Inside the Page Load event, the GridView is populated with some dummy records using a dynamic DataTable.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Exporting GridView to HTML file (HTML string) in ASP.Net
When the Export Button is clicked, the GridView is rendered into an HTML string using the StringWriter, HtmlTextWriter and the RenderControl function.
Then the HTML string is written to the HTTP Response and eventually exported as an HTML (.htm) file.
C#
protected void Export(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
 
            //Download the HTML file.
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.htm");
            Response.Charset = "";
            Response.ContentType = "text/html";
            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)
    Using sw As New StringWriter()
        Using hw As New HtmlTextWriter(sw)
            GridView1.RenderControl(hw)
            Dim sr As New StringReader(sw.ToString())
 
            'Download the HTML file.
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.htm")
            Response.Charset = ""
            Response.ContentType = "text/html"
            Response.Output.Write(sw.ToString())
            Response.Flush()
            Response.End()
        End Using
    End Using
End Sub
 
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered
End Sub
 
 
Screenshots
GridView populated with records
Export GridView to HTML File (HTML String) in ASP.Net using C# and VB.Net
 
GridView exported as HTML (.htm) file
 
Export GridView to HTML File (HTML String) in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads