How to export html table to excel using jQuery Plugin

Hello Friends, I hope you are doing great, Today’s tutorial I am going to show you How to export html table to excel using jQuery Plugin, Jquery is a great tools for UI developer you can do many more this using jquery because there is huge plugin written in jquery to simplify development , So here we’ll learn about table2excel.js plugin written in javascript to export html table to excel quickly.
export-to-excel-jquery
Let’s start the tutorial.



Fist of all include required libraries on page, Note: don’t forget to include jquery code library before table2excel.js because this is dependent on jquery.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="jquery.table2excel.js"></script>

Here is the sample html table, which we are going to export in excel.

<table class="tb2excel" border=1>
<tr><th>S.No.</th><th> Name</th><th> Email</th><th> Phone</th><tr>
<tr><td>1</td><td> Rohit Kumar</td><td> hi@iamrohit.in</td><td> 1000000000</td><tr>
<tr><td>3</td><td> Manish Kumar</td><td> ma@iamrohit.in</td><td> 1000000001</td><tr>
<tr><td>3</td><td> Mohit Kumar</td><td> mo@iamrohit.in</td><td> 1000000011</td><tr>
<tr class="noExl"><td>..</td><td> ..</td><td> ..</td><td> ..</td><tr>
</table>
<h3><a href="#" class="download">Export Excel</a></h3>

In the above table there is class name tb2excel which we’ll use in table2excel function to export table by clicking on Export Excel link.


Finally add table2excel function on click event and make your code actionable.

<script>
$(function() {  
   $(".download").click(function() {  
	$(".tb2excel").table2excel({
				        exclude: ".noExl",
				        name: "Excel Document Name",
					filename: "file_name",
					fileext: ".xls",
					exclude_img: true,
					exclude_links: true,
					exclude_inputs: true
				});
   });
 
});
</script>

You can also exclude selected rows of the table by assigning a simple class to those rows which you don’t want to include in exported excel file, like in my case i assigned .noExl to last rows and pass this class to table2excel function as exclude properties.

See live demo and download sample source code.

DEMO

DOWNLOAD

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.