我定义的js文件为table.js 下面的html中引入
$(document).ready(function () {var base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) };var format = function (s, c) {return s.replace(/{(\w+)}/g,function (m, p) {return c[p];});};var uri = "data:application/vnd.ms-excel;base64,";var template = "<html><head><meta charset='UTF-8'></head><body><table>{table}</table></body></html>";var table = document.getElementById("mytable");//mytable为table标签var ctx = { worksheet: "Worksheet", table: table.innerHTML };//window.location.href = uri + base64(format(template, ctx));document.getElementById("a_export").href = uri + base64(format(template, ctx));document.getElementById("a_export").download = "导出测试.xls";
})
添加按钮
<a id="a_export">导出下方表格数据</a>
结果