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?

Voona Rohila
Kilo Patron
Kilo Patron

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 

find_real_file.png

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
3 REPLIES 3

Krishna  Penaka
Tera Expert

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);

 

}

bravosa
Kilo Guru

In the data type use this

data_type = 'data:application/vnd.ms-excel;base64';

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