export to excel button on a UI page, how do I export a html table from a UI page via export to excel button?

manisha_maurya
Tera Contributor

How do I export a html table from a UI page via export to excel button? I'm trying to have a function called in html of UI page, this function is being called from client script of the same UI page. Where in client script is calling the script include which is having OOTB code for "export to excel context menu". find teh script include below:

script include:

var printing = Class.create();

printing.prototype = Object.extendsObject(AbstractAjaxProcessor, {

runContextAction :   function() {

///log("query"); -------------------------------> when i check the log it is calling the script include.

@

var sysparm_rows = g_list.grandTotalRows;

var num_rows = parseInt(sysparm_rows);

var sysparm_query = g_list.getQuery({orderby: true, fixed: true});

         

gs.log("query=="+sysparm_query);----------------------------------> i dont find any log for this, i believe it is not able to pick up the row count from the UI Page.

   

var sysparm_view = g_list.view;

      if (num_rows < g_export_warn_threshold) {

              var dialog = new GwtPollDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_excel_xlsx');

              dialog.execute();

              return;

      }

      var dialog = new GwtExportScheduleDialog(g_list.tableName, sysparm_query, sysparm_rows, sysparm_view, 'unload_excel_xlsx');

      dialog.execute();  

  return dialog;

},

      type: 'printing'

});

Client script:

function xyz(){

var ga = new GlideAjax('labelprinting');

ga.addParam('sysparm_name','runContextAction');

//ga.addParam('sysparm_user_name',"Bob");

ga.getXML(runContext);

}

function runContext(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    alert(answer);

}

1 ACCEPTED SOLUTION

Nana5
Mega Guru

Hi Manisha,


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


}


Regards,


Prakash Ranjan


View solution in original post

16 REPLIES 16

I'm using IE11 IE ERROR 2-USER AGENT.JPGIE ERROR 3 - APP NAME.JPGerror IE 11.JPG


Hi ..



I'm using IE 11, and please find the code which i'm using.



function fnExcelReport() {


var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";


var textRange;


var j = 0;


tab = document.getElementById('headers'); // 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");




alert(msie);


//if (navigator.appName == 'Microsoft Internet Explorer')


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



alert('sa');




} else //other browser not tested on IE 11


sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));



return sa;


}




Thanks


if (msie > = 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer



now I have made it as :         if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer



I have removed =


Vika1
Giga Contributor

Hello Prakash,

 

Thank you so much for the code!

It seems to be working, but when opening the downloaded file I receive the next error:

find_real_file.png

Any ideas on how it can be solved?

Hi Nana

 

I followed the same code as yours ,all the other cases are working fine and excel is getting downloaded except for row count >=1900.

find_real_file.png

 

 


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