前端自己导出Excel

1、导出当前页面,这是最简单的,只是导出当前页面的数据。

exportData(tableid, name) {
        let table;
        let uri = 'data:application/vnd.ms-excel;base64,',
          template = '{table}
', base64 = function ( s) { return window.btoa(unescape(encodeURIComponent(s))) }, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }; table = document.getElementById(tableid) let ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }; window.location.href = uri + base64(format(template, ctx)) },

2、导出有分页的数据

exportData2() {
      let table;//把所有要导出的数据全部封装成tbody内的数据,然后导出
      let uri = "data:application/vnd.ms-excel;base64,",
        template =
          '{table}
', base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))); }, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }); }; let ctx = { worksheet:"导出", table: table }; window.location.href = uri + base64(format(template, ctx)); },

当前导出的话,我目前在我司的项目上应用,导出超过700条,数据就会在base64处转化失败,建议导出如果超过700条时,可以进行二次封装,每次只是500条,可以存两个变量,一个是{table},一个是base64转化后的数据,然后组合起来,直至数据获取完毕,然后再导出

你可能感兴趣的:(前端自己导出Excel)