Abhilash J A

Abhilash J A

  • 535
  • 2.4k
  • 596.3k

How to Export the jQuery Datatable data to excel in mvc4?

Aug 11 2016 6:41 AM
Hello Everyone,
 
How to Export the jQuery Datatable data to excel in mvc4.
 Now it is exporting 1st page only I want all data export to excel in one button export.
 
function fnExcelReportSecond() {
var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j = 0;
tab = document.getElementById('ListDiv');
for (j = 0 ; j < tab.rows.length ; j++) {
tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
}
var currentURL = window.location.origin;
var pth = currentURL + "/Pic/exportimg.jpg";
tab_text = tab_text + "<table><tr><tr/><tr><tr/><tr><td><td/><td><td/><td><td/><td> <img src=" + pth + " /></td></tr></table>"
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(/<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, "Say Thanks to Sumit.xls");
}
else
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
 
$("#btn_exportlistsecond").click(function () {
fnExcelReportSecond();
}) 

Answers (3)