方法的调用
var json =
'[' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"}' +
']';
var dataJson = JSON.parse(json);
/调用方法
SONToExcelConvertor("我的excel",dataJson);
json转excel 下载 方法#
function JSONToExcelConvertor(fileName, jsonData) {
var arrData = typeof jsonData != 'object' ? JSON.parse(jsonData) : jsonData;
var excel = '';
var row = "";
for (var name in arrData[0]) {
row += "" + name + ' | ';
}
excel += row + "
";
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
var value = arrData[i][index] === "." ? "" : arrData[i][index];
row += '' + value + ' | ';
}
excel += row + "
";
}
excel += "
";
var excelFile = "";
excelFile += '';
excelFile += ';
excelFile += '; charset=UTF-8">';
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);
}