zoukankan      html  css  js  c++  java
  • 前端自己导出Excel

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

    exportData(tableid, name) {
            let table;
            let uri = 'data:application/vnd.ms-excel;base64,',
              template = '<html><head><meta charset="UTF-8"></head><body><table  border="1">{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];
                })
              };
    
            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 =
              '<html><head><meta charset="UTF-8"></head><body><table  border="1">{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];
              });
            };
          let ctx = {
            worksheet:"导出",
            table: table
          };
          window.location.href = uri + base64(format(template, ctx));
        },

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

  • 相关阅读:
    tableview 与 tableview cell
    swift基础知识
    HttpRequest
    ios界面跳转
    C# TextBox常用方法总结
    C#中string.format用法详解
    数据库填充DataSet,逐行访问
    C#连接SQL SERVER数据库的详细步骤!
    高德地图API INVALID_USER_SCODE问题以及keystore问题
    基础地图Android SDK
  • 原文地址:https://www.cnblogs.com/mxyr/p/10824403.html
Copyright © 2011-2022 走看看