javascript - How do I export multiple html tables to excel? -
i have webpage has 3 tables , i'd export 3 of them same excel file. i'd each table in own sheet, having them on same sheet ok well. after googling, i've seen exporting 1 table 1 excel sheet.
var tablestoexcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/tr/rec-html40"><head><!--[if gte mso 9]><xml><x:excelworkbook><x:excelworksheets>' , templateend = '</x:excelworksheets></x:excelworkbook></xml><![endif]--></head>' , body = '<body>' , tablevar = '<table>{table' , tablevarend = '}</table>' , bodyend = '</body></html>' , worksheet = '<x:excelworksheet><x:name>' , worksheetend = '</x:name><x:worksheetoptions><x:displaygridlines/></x:worksheetoptions></x:excelworksheet>' , worksheetvar = '{worksheet' , worksheetvarend = '}' , base64 = function (s) { return window.btoa(unescape(encodeuricomponent(s))) } , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } , wstemplate = '' , tabletemplate = ''; return function (table, name, filename) { var tables = table; (var = 0; < tables.length; ++i) { wstemplate += worksheet + worksheetvar + + worksheetvarend + worksheetend; tabletemplate += tablevar + + tablevarend; } var alltemplate = template + wstemplate + templateend; var allworksheet = body + tabletemplate + bodyend; var allofit = alltemplate + allworksheet; var ctx = {}; (var j = 0; j < tables.length; ++j) { ctx['worksheet' + j] = name[j]; } (var k = 0; k < tables.length; ++k) { var exceltable; if (!tables[k].nodetype) exceltable = document.getelementbyid(tables[k]); ctx['table' + k] = exceltable.innerhtml; } //document.getelementbyid("dlink").href = uri + base64(format(template, ctx)); //document.getelementbyid("dlink").download = filename; //document.getelementbyid("dlink").click(); window.location.href = uri + base64(format(allofit, ctx)); } })();
and html
<html> <head> <title>js excel</title> </head> <body> <table id="1"> <tr><td>hi</td></tr> <tr><td>hey</td></tr> <tr><td>hello</td></tr> </table> <table id="2"> <tr><td>night</td></tr> <tr><td>evening</td></tr> <tr><td>nite</td></tr> </table> <a id="dlink" style="display:none;"></a> <input type="button" onclick="tablestoexcel(['1', '2'], ['first', 'second'], 'myfile.xls')" value="export excel"> <script src="~/views/js/jsexcel.js" type="text/javascript"></script> </body> </html>
note: doesn't work on ie ('data small' error) , on firefox both tables put on same sheet.
credit thread - html table excel javascript