@下载方法封装
/*
* @file 下载功能
* @author whyme23@163.com
*/
const jsonToExcelConvertor = (jsonData, title, fileName) => {
if (!jsonData) {
return;
}
var str = '';
for (var i in title) {
str += `${title[i].title + '\t'},`;
}
str += '\n';
var arrData = typeof jsonData != 'object' ? JSON.parse(jsonData) : jsonData;
for (let i = 0; i < jsonData.length; i++) {
for (var j = 0; j < title.length; j++) {
str += `${arrData[i][title[j].value] === null ? "" : arrData[i][title[j].value] + '\t'},`;
}
str += '\n';
}
// encodeURIComponent解决中文乱码
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
let link = document.createElement("a");
link.href = uri;
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
export default jsonToExcelConvertor;