Hi guys,
I am back with this new article on how to export multiple HTML tables in multiple excel sheets using Javascript. It’s pretty easy. So let’s start to make it :
my tables look like :
<table id="1"> <tr> <th>sample</th> <th>sample</th> <th>sample</th> </tr> <tr> <td>sample</td> <td>sample</td> <td>sample</td> </tr> </table>
<table id="2"> <tr> <th>sample</th> <th>sample</th> <th>sample</th> </tr> <tr> <td>sample</td> <td>sample</td> <td>sample</td> </tr> </table>
Click Here: To Get Code of HTML 5 audio player with playlist using jquery
And I make a button on which I will click to export those tables :
<button onclick="tablesToExcel(array1, array2, 'myfile.xls')" class="btn btn-primary" style="margin-bottom: 15px;"><i class="fa fa-download"></i> Export all to excel</button>
And now comes the part of Javascript. my javascript codes look something like this :
var array1 = new Array(); var array2 = new Array(); var n = 2; //Total table for ( var x=1; x<=n; x++ ) { array1[x-1] = x; array2[x-1] = x + 'th'; } 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; for (var i = 0; i < tables.length; ++i) { wstemplate += worksheet + worksheetvar + i + worksheetvarend + worksheetend; tabletemplate += tablevar + i + tablevarend; } var allTemplate = template + wstemplate + templateend; var allWorksheet = body + tabletemplate + bodyend; var allOfIt = allTemplate + allWorksheet; var ctx = {}; for (var j = 0; j < tables.length; ++j) { ctx['worksheet' + j] = name[j]; } for (var k = 0; k < tables.length; ++k) { var exceltable; if (!tables[k].nodeType) exceltable = document.getElementById(tables[k]); ctx['table' + k] = exceltable.innerHTML; } window.location.href = uri + base64(format(allOfIt, ctx)); } })();
Click Here: To Add facebook & twitter share link in your website
Hope you like this post. Thanks for visiting our blog. Follow our blog site because we have more tutorial related different type of programming languages like PHP, HTML, Javascript, Jquery, CSS3 and get some technology and latest news.
Related posts
This one works perfectly
It is creating worksheet name for each sheet.
But it is exporting all the tables to single sheet only.
How to export to each table to each worksheet.
Rocking chairs hace roⅽked neww mothers with
onee day οld babies, healed broken hearts, and woսnded pride,
and then eased tthe pains of having old.
Skilⅼed florists can certainly pickk an impreѕsiᴠe bouquet which is ⲣerfect foг your selected theme.
Foor smaller famіlies a table with extenders or possibly a
leaf is an ideaⅼ choice because iin the flexibility it gives you.
I actually really liked looking over this and thanks
for the tips – book marked!