Export to excel button on a UI page, how do I export a html table containing more than 2000 rows from a UI page via 'export to excel' button?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-06-2019 11:23 PM
Hi
I exported a html table from a UI page via export to excel button.
Client script in UI Page:
function exportRec() {
var tab_text = "<table border='2px'><tr bgcolor='#33AFFF'>";
var j = 0;
tab = document.getElementById('listProj'); // id of table
for (j = 0; j < tab.rows.length; j++) {
tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
tab_text = tab_text.replace(/Remove/i, "");
}
tab_text = tab_text + "</table>";
tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE");
var sa;
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs", true, "3pi_roster_report.xls");
}
else {//other browser not tested on IE 11
sa = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
sa.href = data_type + ', ' + encodeURIComponent(tab_text);
//setting the file name
sa.download = '3pi_roster_report.xls';
//triggering the function
sa.click();
}
return (sa);
}
The above code is working fine while exporting but when the table contains more than 1900 of rows it is showing below error
Can anyone provide me a solution for this.
I tried replacing else code with below code and its showing blank window.
var sa1 = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa1);
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-06-2019 11:44 PM
Hi,
You can try below code.it should work.
//excel export
function fnExcelReport() {
var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange;
var j = 0;
tab = document.getElementById('tableid'); // 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.replace(/Remove/i, "");
}
tab_text = tab_text + "</table>";
tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
//tab_text = tab_text.replace(/Remove/i, ""); // reomves theremove button label.
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs", true, "test.xls");
} else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-07-2019 06:31 AM
In the data type use this
data_type = 'data:application/vnd.ms-excel;base64';
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-11-2019 10:54 PM
Hi Salvador
I tried this but still file is not getting downloaded.
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP