将HTML表格导出到Excel,将其下载表格内容导出到Excel

时间:2021-01-18 09:57:16

I'm Using This Function while download i need to give default name current date but it's giving default name as download i need to change the name how i can change?

我正在下载时使用此功能我需要提供默认名称当前日期,但它提供默认名称作为下载我需要更改名称我可以如何更改?

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

})()

1 个解决方案

#1


4  

Working Demo

工作演示

Based on this answer you have to do the following:

基于此答案,您必须执行以下操作:

*-Add a hidden link to your html in order to use the download attribute supported by modern browsers:

* - 为您的html添加隐藏链接,以便使用现代浏览器支持的下载属性:

<a id="dlink"  style="display:none;"></a> 

*-Add a third parameter specifying the file name for the function caller, here we will call the file 'myfile.xls':

* - 添加第三个参数,指定函数调用者的文件名,这里我们将调用文件'myfile.xls':

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

*-Add the third parameter to the function deceleration:

* - 将第三个参数添加到功能减速:

 return function (table, name, filename)

*-Remove the following line, we are no longer biding the download to window location href directly.

* - 删除以下行,我们不再直接下载到窗口位置href。

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

*-Add the following 3 lines to replace the removed line, we will assign the download to the hidden link we added to the html, then we will bid that link to the browser default download function which allows filename attribute:

* - 添加以下3行来替换删除的行,我们将下载分配给我们添加到html的隐藏链接,然后我们将该链接出价到浏览器默认下载功能,该功能允许文件名属性:

            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = filename;
            document.getElementById("dlink").click();

#1


4  

Working Demo

工作演示

Based on this answer you have to do the following:

基于此答案,您必须执行以下操作:

*-Add a hidden link to your html in order to use the download attribute supported by modern browsers:

* - 为您的html添加隐藏链接,以便使用现代浏览器支持的下载属性:

<a id="dlink"  style="display:none;"></a> 

*-Add a third parameter specifying the file name for the function caller, here we will call the file 'myfile.xls':

* - 添加第三个参数,指定函数调用者的文件名,这里我们将调用文件'myfile.xls':

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

*-Add the third parameter to the function deceleration:

* - 将第三个参数添加到功能减速:

 return function (table, name, filename)

*-Remove the following line, we are no longer biding the download to window location href directly.

* - 删除以下行,我们不再直接下载到窗口位置href。

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

*-Add the following 3 lines to replace the removed line, we will assign the download to the hidden link we added to the html, then we will bid that link to the browser default download function which allows filename attribute:

* - 添加以下3行来替换删除的行,我们将下载分配给我们添加到html的隐藏链接,然后我们将该链接出价到浏览器默认下载功能,该功能允许文件名属性:

            document.getElementById("dlink").href = uri + base64(format(template, ctx));
            document.getElementById("dlink").download = filename;
            document.getElementById("dlink").click();