json导出为excel(兼容IE)


  import json2csv from "json2csv"
  export function Json2Excel(josnList,fileName) {
    let isIE = false;
    console.log('1----->',josnList);
    if (navigator.userAgent.indexOf("compatible") > -1 && navigator.userAgent.indexOf("MSIE") > -1) { // ie浏览器
      isIE = true;
    }
    if (navigator.userAgent.indexOf("Trident") > -1) { // edge 浏览器
      isIE = true;
    }
    try {
      const result = json2csv.parse(josnList, {
        excelStrings: true
      });
      if (isIE) {
        var BOM = "uFEFF";
        var csvData = new Blob([BOM + result], { type: "text/csv" });
        navigator.msSaveBlob(csvData, `${fileName}.csv`);
      } else {
        const uri = 'data:text/csv;charset=utf-8,ufeff' + encodeURIComponent(result)
        const link = document.createElement('a');
        link.href = uri;
        link.download = `${fileName}.csv`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      }
    } catch (err) {
      console.log(err);
    }
  }

  




更优文章传送门:https://blog.csdn.net/qq_35493664/article/details/88896638
原文地址:https://www.cnblogs.com/LLLLily/p/11277677.html