Search code examples
javascripthtmlexcelfile-conversion

Export large html table to excel using javascript


I am using following code to generate excel file from html table, It is working fine for small scale data set, When it comes to large html table data set it is showing download error.

   //creating a temporary HTML link element (they support setting file names)
    var a = document.createElement('a');
    //getting data from our div that contains the HTML table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('dataTable');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');
    a.href = data_type + ', ' + table_html;
    //setting the file name
    a.download = 'Sample.xls';
    //triggering the function
    a.click();
    //just in case, prevent default behaviour
    e.preventDefault();  

Solution

  • I found one way to solve this issue using FileSaver.js 1: https://github.com/eligrey/FileSaver.js/ Please check followings

    HTML enter image description here

    Javascript

    enter image description here

    this is working fine for me with large data set of HTML table.