表格中的数据自动生成 html,[小记]使用JS将html中table表格数据导出到Excel文档

记录一下

/**

* 描述:导出表格对应到excel文件

* 调用示例:

* onclick = "tableToExcel(tableId,fileName)"

*/

var tableToExcel = (function() {

var uri = 'data:application/vnd.ms-excel;base64,'

, template = '

'+

'

'.excelTable {'+

'border-collapse:collapse;'+

' border:thin solid #999; '+

'}'+

' .excelTable th {'+

' border: thin solid #999;'+

' padding:20px;'+

' text-align: center;'+

' border-top: thin solid #999;'+

' '+

' }'+

' .excelTable td{'+

' border:thin solid #999;'+

' padding:2px 5px;'+

' text-align: center;'+

' }'+'

'

, 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};

var downloadLink = document.createElement("a");

downloadLink.href = uri + base64(format(template, ctx));

downloadLink.download = name+".xls";

document.body.appendChild(downloadLink);

downloadLink.click();

document.body.removeChild(downloadLink);

}

})();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值