如何使用JavaScript将表格数据导出为Excel文件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计151个文字,预计阅读时间需要1分钟。
javascriptfunction exportTableById(tableId) { tableToExcel(tableId);}
var tableToExcel=(function() { var uri='data:application/vnd.ms-excel;base64,'; var template='{table}'; return function(tableId) { var table=document.getElementById(tableId); var=template.replace(/{table}/g, table.innerHTML); var blob=new Blob([], {type: application/vnd.ms-excel}); var url=uri + encodeURI(blob); window.open(url); };})();
将Table 用JS导出成Excelfunction exportTablebyid(tableid) { tableToExcel(tableid); } /** 将div里面的table 导出成excel * */ var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,'; var template = ' {table} '; 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]; }) }; return function(table, name) { if (!table.nodeType) table = document.getElementById(table) table.style='border="1" style="border-color: black;"'; var ctx = { worksheet : name || 'Worksheet', table : table.innerHTML } window.location.href = uri + base64(format(template, ctx)) } })()
本文共计151个文字,预计阅读时间需要1分钟。
javascriptfunction exportTableById(tableId) { tableToExcel(tableId);}
var tableToExcel=(function() { var uri='data:application/vnd.ms-excel;base64,'; var template='{table}'; return function(tableId) { var table=document.getElementById(tableId); var=template.replace(/{table}/g, table.innerHTML); var blob=new Blob([], {type: application/vnd.ms-excel}); var url=uri + encodeURI(blob); window.open(url); };})();
将Table 用JS导出成Excelfunction exportTablebyid(tableid) { tableToExcel(tableid); } /** 将div里面的table 导出成excel * */ var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,'; var template = ' {table} '; 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]; }) }; return function(table, name) { if (!table.nodeType) table = document.getElementById(table) table.style='border="1" style="border-color: black;"'; var ctx = { worksheet : name || 'Worksheet', table : table.innerHTML } window.location.href = uri + base64(format(template, ctx)) } })()

