0

i am tring to export a table from a page to excel sheet. I found a piece of code and it works fine but downloaded file is saved on xls, how can i make it to be save on xlsx? I have problem with turkish characters too, while saving a table, turkish characters are not saved,

my piece of code that i found on internet :

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))
      }
    })()
Gerlav
  • 53
  • 2
  • 8
  • 1
    You can't create XLSX files with this approach. It creates also not really XLS files but SpreadsheetML with embedded HTML. For your problem with turkish characters see http://stackoverflow.com/questions/25730008/encoding-utf-8-when-exporting-html-table-to-excel/25730640#25730640. The HTML has to be UFT-8 encoded and the template in the approach must be expanded with the content-type header. – Axel Richter Jan 28 '15 at 06:30
  • What can i use @Axel Richter for saving file in xlsx ? is there any other approach for it ? – Gerlav Jan 28 '15 at 07:16
  • Internet search will bring some results for "javascript create xlsx file". But nothing I had tested. I suggest use server side (PHP, Java, .NET, ...) solutions instead of client side ones. From those there are many in the wild. – Axel Richter Jan 28 '15 at 07:24
  • Btw @Axel Richterr your solution worked for turkish characters thanks a lot :) – Gerlav Jan 28 '15 at 07:24

1 Answers1

0

XLSX files consist in a bunch of XML files, describing your data and its structure and zipped together (if you rename "myfile.xlsx" to "myfile.zip", you can see what's inside). This means that you can't create such a file client-side since you need access to the file system.

There are a lot of libraries available that you can use to generate XLSX files. What language are you using for the backend?

Adrien
  • 1,929
  • 1
  • 13
  • 23