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

5
  • 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? Commented Feb 11, 2016 at 8:32
  • 2
    is it me or the code will create XLSX file not XLS Commented Feb 12, 2016 at 8:47
  • @Proof: The code creates a mixture of HTML and embedded XML if the Officeversion is greater than or equal 9 <!--[if gte mso 9]>. Excel will accept this mixture as *.xls file and parse the mixture to create a workbook from it. But it will throw that warning. There are JavaScript solutions in the wild which can really creeate XLS and/or XLSX. Search keywords: javascript create excel xls xlsx. Commented Feb 14, 2016 at 8: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 <table> tag will open in the exact same fashion... Commented Feb 16, 2016 at 17:19
  • Take a look here, there you will find all answers. Commented Feb 17, 2016 at 10:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.