[转]纯js导出json到excel(支持chrome)

转自:http://blog.csdn.net/educast/article/details/52775559

function JSONToExcelConvertor(JSONData, FileName, ShowLabel) {
//先转化json
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

var excel = '

';

//设置表头
var row = "

";
for (var i = 0, l = ShowLabel.length; i < l; i++) {
row += "';
}


//换行
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 += '

';
}

excel += row + "

";
}

excel += "

" + ShowLabel[i] + '
' + value + '
";

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);
}

 

//var datas=[];
//var grid=getCmp("grid");
//var grid_data = grid.getStore();

//for(var i= 0 ;i< grid_data.data.length;i++){ //获取extjs grid的数据
//var row = grid_data.getAt(i).data;
//datas.push({"列头1":row["OrgName"],...});
//}

var datas=[{key:value}......];

JSONToExcelConvertor(datas,"汇总",["列头1","头2","头3"]);

 

3252DFE9-8E3E-4CC5-9414-F05F2E6447DA From: http://www.cnblogs.com/xuejianxiyang/p/7827894.html

你可能感兴趣的:(前端)