Transferred from: http://blog.csdn.net/educast/article/details/52775559
function Jsontoexcelconvertor (Jsondata, FileName, ShowLabel) {
Convert JSON first
var arrdata = typeof Jsondata! = ' object '? Json.parse (jsondata): Jsondata;
var excel = ' <table> ';
Set up the table header
var row = "<tr>";
for (var i = 0, L = showlabel.length; i < L; i++) {
Row + = "<td>" + showlabel[i] + ' </td> ';
}
Line break
Excel + = row + "</tr>";
Setting up data
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> ' + value + ' </td> ';
}
Excel + = row + "</tr>";
}
Excel + = "</table>";
var excelfile = "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 + = "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 + = "Excelfile + = "<body>";
Excelfile + = Excel;
Excelfile + = "</body>";
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=[{key:value} ...];
Jsontoexcelconvertor (datas, "Total", ["Column header 1", "Head 2", "Head 3"]);
3252dfe9-8e3e-4cc5-9414-f05f2e6447da from:http://www.cnblogs.com/xuejianxiyang/p/7827894.html
[go] pure JS export JSON to Excel (Chrome supported)