将HTML表格导出到Excel,将其下载表格内容导出到Excel [英] Export HTML table to Excel its downloading table contents to the Excel

查看:66
本文介绍了将HTML表格导出到Excel,将其下载表格内容导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此功能 在下载时,我需要给默认名称提供当前日期,但是在给默认名称作为下载时,我需要更改名称才能更改?

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

})()

解决方案

工作演示

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

*-向您的html添加一个隐藏链接,以使用现代浏览器支持的download属性:

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

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

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

*-将第三个参数添加到函数减速中:

 return function (table, name, filename)

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

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

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

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

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

})()

解决方案

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:

<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':

<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.

 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:

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

这篇关于将HTML表格导出到Excel,将其下载表格内容导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆