将后台返回的json数据转换成excel下载

//json是获取到的数据:
	var json=[{
		id:"1",
		grade:"大二",
		name:"Mike"
	},{
		id:"1",
		grade:"大二",
		name:"Mike"
	}];
	var str = 'ID,年纪,姓名\n'; 
    for (let i = 0; i < jsonData.length; i++) { 
      for (let item in jsonData[i]) { 
        str += `${jsonData[i][item] + '\t,'}`;
      }
      console.log(str);  
      str = str + '\n';
    }  ;
    toLargerCSV(jsonData) { 
        if ('download' in document.createElement('a')) { // 非IE下载 
          var blob = new Blob([str], {
            type: "text/plain;charset=utf-8"
          });
          //解决中文乱码问题
          blob = new Blob([String.fromCharCode(0xFEFF), blob], {
            type: blob.type
          });
          var object_url = window.URL.createObjectURL(blob);
          var link = document.createElement("a");
          link.href = object_url;
          link.download = "数据列表.xls";
          document.body.appendChild(link);
          link.click();
           URL.revokeObjectURL(link.href); // 释放URL 对象
           document.body.removeChild(link);
        } else { // IE10+下载 
          var blob = new Blob([str], {
            type: "text/plain;charset=utf-8"
          });
          //解决中文乱码问题
          blob = new Blob([String.fromCharCode(0xFEFF), blob], {
            type: blob.type
          }); 
          const fileName = '数据列表.xls'; 
          navigator.msSaveBlob(blob, fileName);
        } 
      }

你可能感兴趣的:(js+jquery)