10

This is the function that I use to generate my excel file .xls

 var tableToExcel = (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><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(table, name) {
     if (!table.nodeType) table = document.getElementById(table)
     var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
     window.location.href = uri + base64(format(template, ctx))
    }
})() 

tableToExcel('table', 'Table Title')

And this is the error I get

Error

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Codrina Valo
  • 433
  • 1
  • 5
  • 14
  • have you check the problem that the error implies? extension .xls and the contenttype does it match or have you set them in the wrong way? – Thorarins Feb 11 '16 at 08:32
  • 2
    is it me or the code will create XLSX file not XLS – Poof Feb 12 '16 at 08:47
  • @Proof: The code creates a mixture of `HTML` and embedded `XML` if the `Office`version is greater than or equal 9 ` – Axel Richter Feb 14 '16 at 08:00
  • 2
    that's not an error, it's a warning, and it should not keep the sheet from loading. use CSV output if you want to avoid the warning. and for what it's worth, you don't need all the weird template stuff, a file with just an HTML tag will open in the exact same fashion...
    – dandavis Feb 16 '16 at 17:19
  • Take a look [here](http://stackoverflow.com/questions/5524143/how-can-i-export-tables-to-excel-from-a-webpage), there you will find all answers. – Steffen Mangold Feb 17 '16 at 10:32

1 Answers1

1

The following code takes the table and converts it to the excel file.

Note:- The function have 3rd parameter as the filename.

var tableToExcel = (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><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
    base64 = function(s) {
      return window.btoa(unescape(encodeURIComponent(s)))
    },
    format = function(s, c) {
      return s.replace(/{(\w+)}/g, function(m, p) {
        return c[p];
      })
    }
  return function(table, name, filename) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {
      worksheet: name || 'Worksheet',
      table: table.innerHTML
    }

    document.getElementById("dlink").href = uri + base64(format(template, ctx));
    document.getElementById("dlink").download = filename;
    document.getElementById("dlink").click();

  }
})()
<table id="newTab">
  <th>Col 1</th>
  <th>Col 2</th>
  <th>Col 3</th>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
  </tr>
</table>
<a id="dlink" style="display:none;"></a>

<input type="button" onclick="tableToExcel('newTab', 'name', 'newSheet.xls')" value="Table to Excel">

The following Error might popup while opening the downloaded file.

Error on opening the file. But it opens by clicking 'Yes' anyways.

Error on opening the file. But it opens by clicking 'Yes' anyways.

And the file opens successfully.

File opens successfully.