0

Everyone, I want to set name file for export from html to xls. When I export it get download.xls file when I get download file another name like inventory.xls Please help

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]--><meta 
    http-equiv="content-type" content="text/plain; charset=UTF- 
    8"/> 
    </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))
   }
 })()
srey pick
  • 19
  • 4
  • Possible duplicate of https://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-when-using-data-uri – 04FS Apr 24 '19 at 08:41

1 Answers1

0

Depends how you envisage the user interacting with your page.

If you can somehow tie this functionality to a link or a button, you can use the download attribute,

<a download='FileName' href='your_url'>

which simultaneously suggests a filename for the downloaded resource, and tells the browser that that URI specified is specifically for downloading, rather than navigation, which circumvents you having to use the

window.location.href = uri + base64(format(template, ctx))

part of your code.

Otherwise, this sort of functionality is usually delivered by servers, and being able to set the HTTP headers of the response. e.g. in PHP:

header('Content-Disposition: attachment; filename="July Report.pdf"');

Your function doesn't seem to rely on any dynamic components or inputs, so I would suggest modifying your function to run on page ready, to set the href attribute of a link in the page. Assuming you only ever need one button.

HTML:

<a download="inventory.xls" id="download-link" href=""> Download </a>

JS:

(function() {
  // Inlining these for simplicity. If you need multiple tables and download links on a page, 
  // you can always pull these out later
  let table = 'testTable';
  let name = 'W3C Example Table';

  let 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]--><meta 
    http-equiv="content-type" content="text/plain; charset=UTF- 
    8"/> 
    </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]; 
     })
   }

   if (!table.nodeType) table = document.getElementById(table)
   var ctx = {
      worksheet: name || 'Worksheet', 
      table: table.innerHTML
   }
   document.getElementById("download-link").href = uri + base64(format(template, ctx))
 })()

Obviously I don't know how this is intended to be used, my code will not necessarily work verbatim. But hopefully that helps

SubXaero
  • 367
  • 3
  • 8