Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Table to Excel Javascript

I'm trying to use this script to save a html table to an Excel file, and it works fine, however it doesn't come up in the proper name, but rather with a random string. And I can't see why .

I call it with:

<input type="button" onclick="tableToExcel('tablename', 'name')" value="Export to Excel"> 

code

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)) } })() 
like image 346
Coolcrab Avatar asked Jun 15 '13 18:06

Coolcrab


People also ask

Can you convert HTML to XLS?

To convert an . html file, open it using Excel (File - Open) and then save it as a . xlsx file from Excel (File - Save as).

How do I export dynamically generated HTML table in Excel using jQuery?

First we, will create the HTML table, which shows employee details and an "Export to Excel" button, as shown below. Running the page will look as shown below. Now, we reference the jQuery file and table2excel file in our head section. Now, we write our exportexcel() function, as shown below.


1 Answers

You can use download attribute supported by modern browsera for a anchor element. First modify your HTML by adding an invisible anchor:

<a id="dlink"  style="display:none;"></a>  <input type="button" onclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel"> 

Notice also that the call to function tableToExcel now has 3rd parameter - where you specify file name.

Now use this modified code of your original function:

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();          }     })() 

Notice last 3 code lines: Instead of assigning URL to window - they assign it to the new anchor, then use new download attribute to force download as the given file name and then simple call click() method of the anchor.

Give it a try.

Update - For supporting utf-8 characters

As per the comment below by @WorldSEnder, a simple meta tag in the template would make the excel support utf-8 characters like Hindi.

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"><meta charset="utf-8"/><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>' 
like image 81
Yuriy Galanter Avatar answered Sep 21 '22 05:09

Yuriy Galanter