function JSONToExcelConvertor(JSONData, FileName,errNum,succNum) {
const now = new Date();
let time =now.toLocaleString(‘zh-CN’, {
year: ‘numeric’,
month: ‘2-digit’,
day: ‘2-digit’,
hour: ‘2-digit’,
minute: ‘2-digit’,
second: ‘2-digit’,
hour12: false // 24小时制
}).replace(///g, ‘-’).replace(/\b/g, ‘’);
//先转化json
var arrData = typeof JSONData != ‘object’ ? JSON.parse(JSONData) : JSONData;
var excel = ‘
excel += ‘<td colspan="’ + Object.keys(JSONData[0]).length + ‘"style=“background: #DCDFE6;”>批量导入结果明细\n\n提交时间:’+time+‘\n\n共’+arrData.length+‘条记录\n\n失败’+errNum+‘条,成功’+succNum+‘条’;
var row = “”;
//设置表头
var keys = Object.keys(JSONData[0]);
keys.forEach(function(item) {
row += “’;
});
//换行
excel += row + “”;
//设置数据
// 设置数据
for (var i = 0; i < arrData.length; i++) {
var row = “<tr”;
” + item + ‘ |
// 判断处理结果是否为"处理成功",如果是则设置绿色背景
if (arrData[i]["处理结果"] === "处理成功") {
row += ' style="background-color: #C2E7B0;"'; // 浅绿色背景
}
row += ">";
for (var index in arrData[i]) {
row += '<td>' + arrData[i][index] + '</td>';
}
excel += row + "</tr>";
}
// for (var i = 0; i < arrData.length; i++) {
// var row = "<tr>";
// for (var index in arrData[i]) {
// //var value = arrData[i][index] === "." ? "" : arrData[i][index];
// row += '<td>' + arrData[i][index] + '</td>';
// }
// excel += row + "</tr>";
// }
excel += "</table>";
var excelFile =
"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = FileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
this.$message.success("导出成功");
}