当前位置 : 主页 > 网络编程 > JavaScript >

js json导出到Excel表格

来源:互联网 收集:自由互联 发布时间:2021-06-28
json2excel.js var myjson = { "英文":"English",};var newExcArr = [];//预处理个人的json数据 拼接成如下形式for (var k in myjson) { newArr += '"' + k + '", ' newHtml += ' ' + k + ':"' + myjson[k] + '", ' newExcArr.push([ { "val
json2excel.js
var myjson = {
    "英文":"English",
};
var newExcArr = [];

//预处理个人的json数据 拼接成如下形式
for (var k in myjson) {
    newArr += '"' + k + '", '
    newHtml += '' + k + ':"' + myjson[k] + '",
' newExcArr.push([ { "value": k, "type": "ROW_HEADER" }, { "value": myjson[k], "type": "ROW_HEADER" } ]); } var data = { "title": [ { "value": "中文", "type": "ROW_HEADER_HEADER", "datatype": "string" }, { "value": "英文", "type": "ROW_HEADER_HEADER", "datatype": "string" } ], "data": newExcArr }; //开始导出 JSONToExcelConvertor(data.data, "Report", data.title); 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 += " " + ShowLabel[i].value + ''; } //换行 excel += row + " "; //设置数据 for (var i = 0; i < arrData.length; i++) { var row = " "; for (var index in arrData[i]) { var value = arrData[i][index].value === "." ? "" : arrData[i][index].value; row += ' ' + value + ''; } excel += row + " "; } excel += " "; var sheetName = "英文包"; // 生成sheet表格 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); }
网友评论