3

This code is working fine for 5000 rows in chrome but when table exceed it's size more than 5000. Nothing is exported only in new tab shows #blocked

$("#exportDiv").click(function () {  
    var tab_text = "<table border='2px'><tr bgcolor='#87afc6'>";
    var textrange; var j = 0;
    tab = document.getelementbyid('reporttable'); // id of table
    for (j = 0; j < tab.rows.length; j++) {
        tab_text = tab_text + tab.rows[j].innerhtml + "</tr>";
        //tab_text=tab_text+"</tr>";
    }
    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<a[^>]*>|<\/a>/g, ""); 
    tab_text = tab_text.replace(/<img[^>]*>/gi, ""); 
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); 
    var ua = window.navigator.useragent;
    var msie = ua.indexof("msie ");

    if (msie > 0 || !!navigator.useragent.match(/trident.*rv\:11\./))      
    {
        txtarea1.document.open("txt/html", "replace");
        txtarea1.document.write(tab_text);
        txtarea1.document.close();
        txtarea1.focus();
        sa = txtarea1.document.execcommand("saveas", true, "report.xls");
    }
    else                 //other browser not tested on ie 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeuricomponent(tab_text));

    return (sa);
});
Patryk Falba
  • 417
  • 3
  • 15
  • 3
    It looks like if the `tab_text` is greater than 1.8 million characters it wont create the file. Probably a memory issue. I think this much data should handled in the back-end. – Dvir Levy Aug 22 '19 at 21:30

1 Answers1

1

The URL length that a browser can process is limited. It depends on the browser, but for Chrome it currently is near the 32K limit. Given that some HTML characters get URL-encoded (like &lt;), an encoded HTML table row quickly takes 10 characters, so surely 5000 rows can exceed that browser limit.

Instead of putting the exported data on the URL, let a server-based application produce the content and return it with the proper HTML headers so it opens in a spreadsheet application.

trincot
  • 317,000
  • 35
  • 244
  • 286