Develop an Export to Excel Functionality for UI Page

arnabbose
Tera Expert

Hi,

I want to export the contents of a Custom UI Page that I have built into an Excel. Can we create a button / link that would export of the contents into an Excel?

Please suggest.

find_real_file.png

3 REPLIES 3

jake_mckenna
ServiceNow Employee
ServiceNow Employee

ServiceNow has many build in functions that can export data out of the system into Excel. What is the reason you have chosen to build a custom ui page instead of using the default tables or reports?



If you are looking to combine tables you might also look into building a database view instead. Database Views - ServiceNow Wiki


Nana5
Mega Guru

Hi Arnab,


    You can create a button and export your contents into an Excel.Please find the cose for ur reference.


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+"</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



      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, "download.xls");


      }


      else                                 //other browser not tested on IE 11


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


      return (sa);


}


I get file format error when I used this script.



Any idea how to get rid of this message?