In this article I will explain with an example, how to export ASP.Net Table to Excel file using JavaScript.
The ASP.Net Table will be exported (converted) to Excel file using the jQuery table2excel plugin.
Note: jQuery is used in this article only for the use of jQuery plugin.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net Table and an ASP.Net Button.
The Button has been assigned with a JavaScript OnClientClick event handler.
<asp:Table ID="tblCustomers" runat="server">
    <asp:TableHeaderRow>
        <asp:TableHeaderCell>Customer Id</asp:TableHeaderCell>
        <asp:TableHeaderCell>Name</asp:TableHeaderCell>
        <asp:TableHeaderCell>Country</asp:TableHeaderCell>
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell>1</asp:TableCell>
        <asp:TableCell>John Hammond</asp:TableCell>
        <asp:TableCell>United States</asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell>2</asp:TableCell>
        <asp:TableCell>Mudassar Khan</asp:TableCell>
        <asp:TableCell>India</asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell>3</asp:TableCell>
        <asp:TableCell>Suzanne Mathews</asp:TableCell>
        <asp:TableCell>France</asp:TableCell>
    </asp:TableRow>
    <asp:TableRow>
        <asp:TableCell>4</asp:TableCell>
        <asp:TableCell>Robert Schidner</asp:TableCell>
        <asp:TableCell>Russia</asp:TableCell>
    </asp:TableRow>
</asp:Table>
<br/>
<asp:Button ID="btnExport" Text="Export" runat="server" OnClientClick="return Export();"/>
 
 
Exporting ASP.Net Table to Excel using JavaScript
When the Export Button is clicked, the JavaScript Export function is called.
Inside the Export function, the jQuery table2excel plugin is applied to the ASP.Net 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() {
        $("[id*=tblCustomers]").table2excel({
            filename: "Table.xls"
        });
        return false;
    }
</script>
 
 
Screenshots
The ASP.Net Table
Export ASP.Net Table to Excel using JavaScript
 
Excel file being downloaded when Export Button is clicked
Export ASP.Net Table to Excel using JavaScript
 
The generated Excel file
Export ASP.Net Table to Excel using JavaScript
 
 
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