If you want to achieve it, simply embark on a journey of exploration and study. Here are some suggestions for you to consider and adapt the codes according to your requirements.
This technique is compatible with IE7+, Firefox, Chrome, and Safari. Below is the JavaScript code:
function ToExcelReport(){
var tab_text="<table border='2px'><tr bgcolor='#FFFFFF'>";
var textRange; var j=0;
tab = document.getElementById('tb1'); //id of table
for(j = 0 ; j < tab.rows.length ; j++){
tab_text=tab_text+tab.rows[j].innerHTML+"</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, ""); //remove 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,"Sample.xls");
} else{
//other browsers
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
}
return (sa);
}
Create an empty iframe:
<iframe id="sample" style="display:none"></iframe>
Then design a button that triggers the export process. (It's up to you)
<button id="btnExport" onclick="ToExcelReport();"> EXPORT </button>
Here's another approach using jQuery. This method involves using jquery
in conjunction with the JQuery DataTables Table Tools plugin. Keep in mind that this may not function correctly without flash support.
$(document).ready( function () {
$('#tb1').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
}
} );
} );
Lastly, one of the most effective ways is to utilize server-side languages. Sometimes, dealing with client-side languages can be challenging in situations like this. If you're open to new perspectives and willing to make a change, consider using PHPExcel :)