Export HTML Table Data to CSV using JavaScript

The export feature is very useful on data list in a web application. It helps the user to download data list as a file format for offline use. In the web application, CSV is a most used format for exporting data to a file. Export data to CSV is very easy and most important to make your web application user-friendly. Basically, the export data functionality is used in the member list, product list, or other lists to download data list in file format.

Using JavaScript, you can easily export data to CSV file without using any jQuery plugin. In this tutorial, we’ll show you how to export HTML table data to CSV using JavaScript. Here we’ll implement export functionality for export members data to CSV file.

JavaScript Code

The following JavaScript code contains 2 functions, named downloadCSV() and exportTableToCSV().
The downloadCSV() function takes CSV data and generate download link to download HTML table data in a CSV file.

function downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV file
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();
}

The exportTableToCSV() function creates CSV data from table HTML and download CSV data as a file by using the downloadCSV() function.

function exportTableToCSV(filename) {
    var csv = [];
    var rows = document.querySelectorAll("table tr");
    
    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll("td, th");
        
        for (var j = 0; j < cols.length; j++) 
            row.push(cols[j].innerText);
        
        csv.push(row.join(","));        
    }

    // Download CSV file
    downloadCSV(csv.join("\n"), filename);
}

HTML Code

The members HTML table contains some basic users data, like name, email, country.

<table>
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Country</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>john@gmail.com</td>
        <td>USA</td>
    </tr>
    <tr>
        <td>Stephen Thomas</td>
        <td>stephen@gmail.com</td>
        <td>UK</td>
    </tr>
    <tr>
        <td>Natly Oath</td>
        <td>natly@gmail.com</td>
        <td>France</td>
    </tr>
</table>

On clicking the button, exportTableToCSV() method is called to export table data to CSV file. Also, the desired filename for download CSV file is passed to this function.

<button onclick="exportTableToCSV('members.csv')">Export HTML Table To CSV File</button>

Conclusion

Hope using our minimal JavaScript code you can easily export table data to CSV. You don’t need to user any jQuery plugin or server side script for export data to CSV. Also, you can add table columns and rows based on your requirement.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

25 Comments

  1. Oleksandr Said...
  2. Ciotous Said...
  3. Yami Leth Said...
  4. Shiva Said...
  5. Anisha Said...
  6. Ronny Said...
  7. Briarpatch Dragon Said...
  8. Richard Said...
  9. Jerome Said...
  10. Sora Dale Said...
  11. Alekhya Said...
  12. A Said...
  13. Abraham Said...
  14. Helsy Said...
  15. Yohan Said...
  16. Timi Said...
  17. Rose Said...
  18. Abhishek Sharma Said...
  19. Cristopher Said...
  20. Dorian Said...
  21. Rivanis Said...
  22. Josef Said...
  23. Daniel Said...
  24. Velu Said...
  25. Boniface Maina Said...

Leave a reply

keyboard_double_arrow_up