Export HTML Table Data to Excel using JavaScript

Export data to Excel is very useful on the data list for nearly every web application. The export feature helps to download the data list as a file format for offline use. Excel format is ideal for exporting data in a file. Mostly the server-side method is used for export data to excel using PHP. But if you want a client-side solution to export table data to excel, it can be easily done using JavaScript.

The client-side export functionality makes the web application user-friendly. Using JavaScript, the HTML table data can be easily exported without page refresh. In this tutorial, we will show you how to export HTML table data to excel using JavaScript. The JavaScript export functionality can be used in the member list, product list, or other lists to download the data list in excel file format.

Export HTML Table Data to Excel

JavaScript Code:
The exportTableToExcel() function convert HTML table data to excel and download as XLS file (.xls).

  • tableID – Required. Specify the HTML table ID to export data from.
  • filename – Optional. Specify the file name to download excel data.
function exportTableToExcel(tableID, filename = ''){
    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    
    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
}

HTML Table Data:
The HTML table contains some users data with some basic fields like name, email, country.

<table id="tblData">
    <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>Michael Addison</td>
        <td>michael@gmail.com</td>
        <td>UK</td>
    </tr>
    <tr>
        <td>Sam Farmer</td>
        <td>sam@gmail.com</td>
        <td>France</td>
    </tr>
</table>

The button triggers exportTableToExcel() function to export HTML table data using JavaScript.

<button onclick="exportTableToExcel('tblData')">Export Table Data To Excel File</button>

If you want to export data with the custom file name, pass your desired file name in the exportTableToExcel() function.

<button onclick="exportTableToExcel('tblData', 'members-data')">Export Table Data To Excel File</button>

Export Data to Excel in PHP

Conclusion

Our example code helps you to add export functionality in the HTML table without any third-party jQuery plugin or server-side script. You can easily export the table data using minimal JavaScript code. Also, the functionality of the example code can be enhanced or customized easily as per your needs.

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

43 Comments

  1. Brindha Said...
  2. Spyros Said...
  3. Isaki Said...
  4. Jignesh Said...
  5. Well Said...
  6. Amit Das Said...
  7. Ankit Gupta Said...
  8. Dhaval Said...
  9. Didier Marcel Said...
  10. WAN Said...
  11. Tafs Said...
  12. Jennifer Said...
  13. Soham Said...
  14. A. Henket Said...
  15. Pascal Said...
  16. Esh Esh Said...
  17. Deepak Gupta Said...
  18. Damcho Said...
  19. Emanuel Said...
  20. Liam Bryce Said...
  21. Nel Campos Said...
  22. David Said...
  23. Nitesh Malviya Said...
  24. Levin Said...
  25. Saurav Purohit Said...
  26. Liupo Said...
  27. Gianmarco_99 Said...
    • CodexWorld Said...
  28. Michael Kagiri Said...
  29. Krithika Said...
  30. Alpesh Katariya Said...
  31. Jeremiah Said...
  32. Shubham Gupta Said...
  33. Abdul Basit Said...
  34. Anthony Said...
  35. Berka Said...
  36. MSK Said...
  37. Dileep Kumar Said...
  38. Vicks Said...
  39. Tonny Said...
  40. Pawan Said...

Leave a reply

keyboard_double_arrow_up