Skip to main content

Exporting/Download HTML Table as CSV File Using jQuery

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>
 <br />
 <button type="button" id="btnDownload"> Download as CSV</button>


Use below script to download the html table as csv file

<script type="text/javascript">
$(document).ready(function() {
$('#btnDownload').click(function() {
filename: 'CustomerList'

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.

<script type="text/javascript" src="jquery.min.js" />
<script type="text/javascript" src="/jquery.tabletocsv.js" />

Output will be look like as below


You can download working example from this link

Tarun Dudhatra

Tarun is a writer of website. He is a software professional with hands on 10+ years of experience. Generally he writes an article about microsoft technology, Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.