Whenever we are working on any project generally we need to display data into the grid, and often we need to download those records as a excel or CSV file. So in our today’s article we will see how to download/export html table data as CSV file using JQuery.
In the jQuery there is a plugin called “tabletoCSV” which is very easy to use and it will done the task done.
Now lets see below I have created a simple “HTML table with Export as CSV button”
<form id="myform" runat="server">
<table id="tabletodownload" style="border:1px solid #000;">
<tr> <th> Customer Name </th> <th> Order Value </th> <th> Ordered On </th> </tr>
<tr> <td> Tarun </td> <td> $ 5,000 </td> <td> 7, Oct 2017 </td> </tr>
<tr> <td> James <td> <td> $ 4,000 </td> <td> 10, Oct 2017 </td> </tr>
<tr> <td> Vijay </td> <td> $ 3,000 </td> <td> 29, Sept 2017 </td> </tr>
<tr> <td> Andy </td> <td> $ 2,000 </td> <td> 22, Oct 2017 </td> </tr>
<tr> <td> Andrew </td> <td> $ 1,000 </td> <td> 29, Oct 2017 </td> </tr>
<button type="button" id="btnDownload"> Download as CSV</button>
Use below script to download the html table as csv file
If we run this page in the browser then it will display like below screen and you can export tables as CSV by clicking on the ‘Download as CSV‘ button.
For the above example we need to reference to js file 1 is the JQuery file and another is the “tabletoCSV” plugin js file.
Output will be look like as below
You can download working example from this link