Advertisement
LrdArc

Untitled

Aug 3rd, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.43 KB | None | 0 0
  1. <table id="1">
  2.     <tr><td>Hi</td></tr>
  3.     <tr><td>Hey</td></tr>
  4.     <tr><td>Hello</td></tr>
  5. </table>
  6. <table id="2">
  7.     <tr><td>Night</td></tr>
  8.     <tr><td>Evening</td></tr>
  9.     <tr><td>Nite</td></tr>
  10. </table>
  11.  
  12. <a id="dlink"  style="display:none;"></a>
  13. <input type="button" onclick="tablesToExcel(array1, array2, 'myfile.xls')" value="Export to Excel">
  14.  
  15. <script>
  16.     var array1 = new Array();
  17.     var array2 = new Array();
  18.     var n = 2;
  19.     for ( var x=1; x<=n; x++ ) {
  20.         array1[x-1] = x;
  21.         array2[x-1] = x + 'th';
  22.     }
  23.  
  24.     var tablesToExcel = (function () {
  25.         var uri = 'data:application/vnd.ms-excel;base64,'
  26.         , 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>'
  27.         , templateend = '</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>'
  28.         , body = '<body>'
  29.         , tablevar = '<table>{table'
  30.         , tablevarend = '}</table>'
  31.         , bodyend = '</body></html>'
  32.         , worksheet = '<x:ExcelWorksheet><x:Name>'
  33.         , worksheetend = '</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>'
  34.         , worksheetvar = '{worksheet'
  35.         , worksheetvarend = '}'
  36.         , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
  37.         , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
  38.         , wstemplate = ''
  39.         , tabletemplate = '';
  40.  
  41.         return function (table, name, filename) {
  42.             var tables = table;
  43.  
  44.             for (var i = 0; i < tables.length; ++i) {
  45.                 wstemplate += worksheet + worksheetvar + i + worksheetvarend + worksheetend;
  46.                 tabletemplate += tablevar + i + tablevarend;
  47.             }
  48.  
  49.             var allTemplate = template + wstemplate + templateend;
  50.             var allWorksheet = body + tabletemplate + bodyend;
  51.             var allOfIt = allTemplate + allWorksheet;
  52.  
  53.             var ctx = {};
  54.             for (var j = 0; j < tables.length; ++j) {
  55.                 ctx['worksheet' + j] = name[j];
  56.             }
  57.  
  58.             for (var k = 0; k < tables.length; ++k) {
  59.                 var exceltable;
  60.                 if (!tables[k].nodeType) exceltable = document.getElementById(tables[k]);
  61.                 ctx['table' + k] = exceltable.innerHTML;
  62.             }
  63.  
  64.             //document.getElementById("dlink").href = uri + base64(format(template, ctx));
  65.             //document.getElementById("dlink").download = filename;
  66.             //document.getElementById("dlink").click();
  67.  
  68.             window.location.href = uri + base64(format(allOfIt, ctx));
  69.  
  70.         }
  71.     })();
  72. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement