In this article I will explain with an example, how to export HTML string to Microsoft Excel file in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an HTML Table inside an HTML DIV, a Hidden Field and a Button.
The Button has been assigned a jQuery Click event handler to copy the contents of HTML DIV to the Hidden Field, so that the HTML string can be sent to the Server.
<div id="Grid">
    <table cellspacing="0" cellpadding="2" style="border-collapse: collapse;border: 1px solid #ccc;font-size: 9pt;">
        <tr>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Customer Id</th>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Name</th>
            <th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">1</td>
            <td style="width:150px;border: 1px solid #ccc">John Hammond</td>
            <td style="width:120px;border: 1px solid #ccc">United States</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">2</td>
            <td style="width:150px;border: 1px solid #ccc">Mudassar Khan</td>
            <td style="width:120px;border: 1px solid #ccc">India</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">3</td>
            <td style="width:150px;border: 1px solid #ccc">Suzanne Mathews</td>
            <td style="width:120px;border: 1px solid #ccc">France</td>
        </tr>
        <tr>
            <td style="width:120px;border: 1px solid #ccc">4</td>
            <td style="width:150px;border: 1px solid #ccc">Robert Schidner</td>
            <td style="width:120px;border: 1px solid #ccc">Russia</td>
        </tr>
    </table>
</div>
<br />
<asp:HiddenField ID="hfGridHtml" runat="server" />
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="ExportToExcel" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnExport]").click(function () {
            $("[id*=hfGridHtml]").val($("#Grid").html());
        });
    });
</script>
 
 
Exporting HTML string to EXCEL
When the Export Button is clicked, the value of the HTML string is extracted from the Hidden Field. Then the HTML string is export to Microsoft Excel file by writing the HTML string to the Output Response.
C#
protected void ExportToExcel(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=HTML.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    Response.Output.Write(Request.Form[hfGridHtml.UniqueID]);
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub ExportToExcel(sender As Object, e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=HTML.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Response.Output.Write(Request.Form(hfGridHtml.UniqueID))
    Response.Flush()
    Response.End()
End Sub
 
 
Error
You might receive the following error when you click on the Export Button.
A potentially dangerous Request.Form value was detected from the client.
 
 
Screenshot
HTML Table
Export HTML string to Excel file in ASP.Net using C# and VB.Net
 
Exported Excel
Export HTML string to Excel file in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads