function exportExcel(JSONData, FileName, title, filter) {
if (!JSONData) return;
//转化json为object
var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;
var excel = "
";
//设置表头
var row = "";
if (title) { //使用标题项
for (var i in title) {
row += "" + title[i] + " | ";
}
} else {//不使用标题项
for (var i in arrData[0]) {
row += "" + i + " | ";
}
}
excel += row + "
";
//设置数据
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
//判断是否有过滤行
if (filter) {
if (filter.indexOf(index) == -1) {
var value = arrData[i][index] == null ? "" : arrData[i][index];
row += "" + value + " | ";
}
} else {
var value = arrData[i][index] == null ? "" : arrData[i][index];
row += "" + value + " | ";
}
}
excel += row + "
";
}
excel += "
";
var excelFile =
"";
excelFile +=
'';
excelFile +=
'';
excelFile += "";
excelFile += "";
excelFile += "";
excelFile += "";
excelFile += excel;
excelFile += "";
excelFile += "";
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);
}