繁体   English   中英

如何将下载文件的名称从export html设置为xls?

[英]How can I set name of download file from export html to xls?

每个人,我想设置名称文件从html导出到xls。 当我导出它得到download.xls文件时,我得到下载文件另一个名称像inventory.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]--><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))
   }
 })()

取决于您如何设想用户与您的页面进行交互。

如果您能以某种方式将此功能与链接或按钮联系起来,则可以使用download属性,

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

同时建议下载资源的文件名,并告诉浏览器指定的URI是专门用于下载而不是导航,这绕过了你不得不使用

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

部分代码。

否则,这种功能通常由服务器提供,并且能够设置响应的HTTP头。 例如在PHP中:

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

您的函数似乎不依赖于任何动态组件或输入,因此我建议您修改函数以在页面就绪时运行,以设置页面中链接的href属性。 假设你只需要一个按钮。

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

显然我不知道这是如何使用的,我的代码不一定会逐字工作。 但希望这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM