In this article I will explain with an example, how to export HTML Table to Excel file using JavaScript in ASP.Net.
The HTML Table will be exported (converted) to Excel file using the jQuery table2excel plugin in ASP.Net.
Note: jQuery is used in this article only for the use of jQuery plugin.
 
 
HTML Markup
The HTML Markup consists of an HTML Table and a Button.
The Button has been assigned with a JavaScript OnClientClick event handler.
<table id="tblCustomers">
    <tr>
        <th>Customer Id</th>
        <th>Name</th>
        <th>Country</th>
    </tr>
    <tr>
        <td>1</td>
        <td>John Hammond</td>
        <td>United States</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Mudassar Khan</td>
        <td>India</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Suzanne Mathews</td>
        <td>France</td>
    </tr>
    <tr>
        <td>4</td>
        <td>Robert Schidner</td>
        <td>Russia</td>
    </tr>
</table>
<br/>
<asp:Button ID="btnExport" Text="Export" runat="server" OnClientClick="return Export();" />
 
 
Exporting HTML Table to Excel using JavaScript in ASP.Net
When the Export Button is clicked, the JavaScript Export function is called.
Inside the Export function, the jQuery table2excel plugin is applied to the HTML Table and it is converted (exported) to Excel file.
The jQuery table2excel plugin accepts filename parameter which sets the name of the Excel file.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="Scripts/table2excel.js"></script>
<script type="text/javascript">
    function Export() {
        $("#tblCustomers").table2excel({
            filename: "Table.xls"
        });
        retuen false;
    }
</script>
 
 
Screenshots
The HTML Table
Export HTML Table to Excel using JavaScript in ASP.Net
 
Excel file being downloaded when Export Button is clicked
Export HTML Table to Excel using JavaScript in ASP.Net
 
The generated Excel file
Export HTML Table to Excel using JavaScript in ASP.Net
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads