Last Updated on August 17, 2022 by Roshan Parihar
In this tutorial, learn how to convert HTML table to excel sheet in jQuery. The short answer is the use the jQuery plugin ‘table2excel’ to export the HTML table content into excel format.
There can be many other useful methods for allowing users to download the table data to their local system in excel format. Let’s find out with the examples given below.
How to Convert HTML Table to Excel Using jQuery
To convert your tabular form data into an excel spreadsheet, the jQuery ‘table2excel’ plugin is the easiest method to include in your project. You just have to use a simple script to create downloads for your users in excel format.
Firstly, you have to include the jQuery library ‘jquery.min.js’ in your project. After that, include the plugin JS file with the name ‘jquery.table2excel.js’ as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://tutorialdeep.com/js/jquery.table2excel.js"></script> <table id="mytable2excel"> <tr> <th>Sr No.</th> <th>Cricket Name</th> <th>Country</th> <th>Score</th> </tr> <tr> <td>1</td> <td>Sachin Tendulkar</td> <td>India</td> <td>132*</td> </tr> <tr> <td>2</td> <td>Virat Kohli</td> <td>India</td> <td>169</td> </tr> <tr> <td>3</td> <td>Jonny Bairstow</td> <td>England</td> <td>102*</td> </tr> <tr> <td>4</td> <td>Rohit Sharma Tendulkar</td> <td>India</td> <td>132*</td> </tr> <tr> <td>5</td> <td>David Warner</td> <td>Australia</td> <td>151*</td> </tr> </table> <button id="exportpdf">Export</button> <script> $("button#exportpdf").click(function(){ $("#mytable2excel").table2excel({ name:"Worksheet Name", filename:"SomeFile",//your filename without extension fileext:".xls" // file extension }); }); </script> |
Output
Sr No. | Cricket Name | Country | Score |
---|---|---|---|
1 | Sachin Tendulkar | India | 132* |
2 | Virat Kohli | India | 169 |
3 | Jonny Bairstow | England | 102* |
4 | Rohit Sharma Tendulkar | India | 132* |
5 | David Warner | Australia | 151* |
Click the above button to export the table in excel spreadsheet format
It also requires including a small script just below the table and button. The script contains the click event on the button element and the table selector using table id. After that, use the table2excel()
within which specify the file name and the extension ‘.xls’.
Exclude Table Rows or Cells From the Excel Sheet Using jQuery
If you want to exclude table cells from the excel export file, you have to specify the class to the cells and add a line to the small script as exclude: ".yourclassname"
. This will exclude the cells that contain the specified classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://tutorialdeep.com/js/jquery.table2excel.js"></script> <table id="mytable2excel"> <tr> <th class="noExl">Sr No.</th> <th>Cricket Name</th> <th>Country</th> <th>Score</th> </tr> <tr> <td class="noExl">1</td> <td>Sachin Tendulkar</td> <td>India</td> <td>132*</td> </tr> <tr> <td class="noExl">2</td> <td>Virat Kohli</td> <td>India</td> <td>169</td> </tr> <tr> <td class="noExl">3</td> <td>Jonny Bairstow</td> <td>England</td> <td>102*</td> </tr> <tr> <td class="noExl">4</td> <td>Rohit Sharma Tendulkar</td> <td>India</td> <td>132*</td> </tr> <tr> <td class="noExl">5</td> <td>David Warner</td> <td>Australia</td> <td>151*</td> </tr> </table> <button id="exportpdf">Export</button> <script> $("button#exportpdf").click(function(){ $("#mytable2excel").table2excel({ //exclude CSS class exclude:".noExl", name:"Worksheet Name", filename:"SomeFile",//your filename without extension fileext:".xls" // file extension }); }); </script> |
Output
Sr No. | Cricket Name | Country | Score |
---|---|---|---|
1 | Sachin Tendulkar | India | 132* |
2 | Virat Kohli | India | 169 |
3 | Jonny Bairstow | England | 102* |
4 | Rohit Sharma Tendulkar | India | 132* |
5 | David Warner | Australia | 151* |
Click the above button to export the table in excel spreadsheet format
When you click the button given above, you will get the download in excel format with a table excluding the cell with the specified class.
So, ‘table2excel’ is the simple jQuery plugin to easily convert your HTML tables into excel format downloads.