以下仅供参考~ 效果图
function exportCvs(dataObj) {
var title = dataObj.title;
var jsonKey = dataObj.jsonKey;
var data = dataObj.data;
var str = [];
str.push(dataObj.title.join(",") + "\n");
for (var i = 0; i < data.length; i++) {
var temp = [];
for (var j = 0; j < jsonKey.length; j++) {
temp.push(data[i][jsonKey[j]]);
}
str.push(temp.join(",") + "\n");
}
var uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str.join(""));
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = new Date().toISOString().substring(0, 10) + "-" + dataObj.fileName + "数据明细.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};
var data = [
{
id: "1",
Name: "张三",
age: "23"
},{
id: "2",
Name: "小明",
age: "12"
},{
id: "3",
Name: "兔兔",
age: "13"
}
]
var storageObj = {
title:["ID","姓名","年龄"],
jsonKey:["id", "Name","age"],
data: data,
fileName: "数据明细"
};
exportCvs(storageObj)
|