js 导出Excel

 

let arr =[{},{}];
let str = '用户id,手机,卡号,昵称,姓名,年龄,性别,身高cm,体重kg';
for(let i=0; i<arr.length; i++){
  str += "\n"+ arr[i].userId +','+ arr[i].account +','+ arr[i].cardId +','+ arr[i].name +','+ arr[i].realName +','+ arr[i].age +','+ (arr[i].sex=='0' ?'女': arr[i].sex=='1' ?'男': '未知')+','+ arr[i].height +','+ arr[i].weight
}
var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str);
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "export.csv";

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);

posted @ 2017-10-25 19:01  lijun8637  阅读(123)  评论(0编辑  收藏  举报