[Go] Pure JS export JSON to Excel (Chrome supported)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.