There are ways on how you will achieve it, you just need to explore and study. Here are my suggestions. Please examine these and alter the codes to suit your need.
This method works with IE7+, Firefox, Chrome and Safari. Here's the javascript:
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);
}
Now create a blank iframe:
<iframe id="sample" style="display:none"></iframe>
Then create a button which triggers the exportation. (It depends on you)
<button id="btnExport" onclick="ToExcelReport();"> EXPORT </button>
And here's another method. Do it in jquery
way. (Jquery DataTables Table Tools plugin) But the cons is it will not work properly if by chance you don't have flash.
$(document).ready( function () {
$('#tb1').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
}
} );
} );
Lastly, the excellent way I know is to use server-side language (because sometimes client-side languages is a pain when it comes to situation like this). If you have time to think and change your view in life... Joke!... use PHPExcel :)