打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
json转换成excel在线js小工具分享【不限制大小】

操作也是差不多,支持直接输入json和文件上传。可以导出xls,也可以导出xlsx。如果你的数据比较多的话,建议使用firefox浏览器,导出为xlsx。之所以建议你用导出xlsx,是因为如果导出的行数超过6w的话,xls是不支持的。而推荐使用firefox的原因是因为我的代码实现方式,需要用到dataURI。各个浏览器,对它的大小有限制。firefox则是无限制。使用它导出15M的json,都是秒级的,很快。

大家可以体验下http://j2e.kpoda.com/

硬货

以下是主要代码

function exportJson2Excel(json, type) {      //TODO 记录导出操作日志      var log = {"type": "json2excel"};//title      try {        var title = new Set();        for (var i = 0; i < json.length; i++) {          var r = json[i];          getProFromObject(r, title);        }        console.log("title", title);        var data = [];        for (var i = 0; i < json.length; i++) {          var r = json[i];          var dataRow = [];          title.forEach(function (t) {            var d1 = r[t];            var ss = t.split(".");            if (ss.length >= 2) {              var tmp = r;              for (var i = 0; i < ss.length; i++) {                var s = ss[i];                tmp = tmp[s];                if (!tmp) {                  break;                }              }              d1 = tmp;            }            if (d1) {              if (typeof d1 == 'object') {                dataRow.push(JSON.stringify(d1));              } else {                dataRow.push(d1);              }            } else {              dataRow.push("");            }          });          data.push(dataRow);        }        console.log("data", data);        jsonToExcelConvertor(data, 'Report', Array.from(title), type);      } catch (err) {        console.error(err);        alert("导出报错:" + err.stack);        log.error = err.stack;        log.json =  json;      } finally {        OplogsService.save(log).$promise.then(function (res) {          console.log(res);        }).catch(function (error) {          console.log(error);          alert("系统错误:" + JSON.stringify(error));        });      }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

getProFromObject方法用来获取json对象的属性,其对应excel的列。其中用到了递归。

function getProFromObject(r, title, parentsPros) {      for (var rp in r) {        if (parentsPros) {          title.add(parentsPros + "." + rp);        } else {          title.add(rp);        }        if (typeof r[rp] == 'object') {          if (parentsPros) {            getProFromObject(r[rp], title, parentsPros + "." + rp);          } else {            getProFromObject(r[rp], title, rp);          }        }      }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

jsonToExcelConvertor方法用于把整理好的数据,转换为DataURI,输出excel。

function jsonToExcelConvertor(JSONData, FileName, ShowLabel, type) {      type = type ? type : "xls";      var application = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";      if (type == "xls") {        application = "application/vnd.ms-excel";      }      // 先转化json      var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;      var excel = '<table>';      // 设置表头      var rr = '<tr>';      for (var j = 0, l = ShowLabel.length; j < l; j++) {        rr += '<td>' + ShowLabel[j] + '</td>';      }      // 换行      excel += rr + '</tr>';      // 设置数据      for (var i = 0; i < arrData.length; i++) {        var row = '<tr>';        for (var index = 0; index < arrData[i].length; index++) {          var value = arrData[i][index] === '.' ? '' : arrData[i][index];          row += '<td>' + value + '</td>';        }        excel += row + '</tr>';      }      excel += '</table>';      var excelFile = '<html xmlns:o=\'urn:schemas-microsoft-com:office:office\' xmlns:x=\'urn:schemas-microsoft-com:office:excel\' xmlns=\'http://www.w3.org/TR/REC-html40\'>';      excelFile += '<meta http-equiv="content-type" content="' + application + '; charset=UTF-8">';      excelFile += '<meta http-equiv="content-type" content="' + application;      excelFile += '; charset=UTF-8">';      excelFile += '<head>';      excelFile += '<!--[if gte mso 9]>';      excelFile += '<xml>';      excelFile += '<x:ExcelWorkbook>';      excelFile += '<x:ExcelWorksheets>';      excelFile += '<x:ExcelWorksheet>';      excelFile += '<x:Name>';      excelFile += '{worksheet}';      excelFile += '</x:Name>';      excelFile += '<x:WorksheetOptions>';      excelFile += '<x:DisplayGridlines/>';      excelFile += '</x:WorksheetOptions>';      excelFile += '</x:ExcelWorksheet>';      excelFile += '</x:ExcelWorksheets>';      excelFile += '</x:ExcelWorkbook>';      excelFile += '</xml>';      excelFile += '<![endif]-->';      excelFile += '</head>';      excelFile += '<body>';      excelFile += excel;      excelFile += '</body>';      excelFile += '</html>';      var uri = 'data:' + application + ';charset=utf-8,' + encodeURIComponent(excelFile);      var link = document.createElement('a');      link.href = uri;      //TODO Cannot set property style of #<HTMLElement> which has only a getter      // link.style = 'visibility:hidden';      $(link).css("visibility", "hidden");      // link.download = FileName + '.'+type;      $(link).attr("download", FileName + '.' + type);      document.body.appendChild(link);      link.click();      document.body.removeChild(link);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
js中数组,对象,json区别
VBA编程基础:对象与集合
application
HttpClient + ASP.NET Web API, WCF之外的另一个选择
WebAPI IdentityServer4身份验证及客户端访问实现
delphi7调用RESTful API,Json的应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服