javascript将json格式数组下载为excel表格的方法

发布于 2020-7-27|标签javascript
复制链接
下面小妖就为大家分享一篇javascript将json格式数组下载为excel表格的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小妖过来看看吧
实例如下: ```xhtml $(document).ready(function(){ $('#wwo').click(function(){ var data = { "title": [ {"value":"A1标题"}, {"value":"B1标题"} ], "data": [ [ {"value":"好好"}, {"value":"2015-08-24"} ], [ {"value":"123"}, {"value":"hahah"} ] ] }; if(data == ''){ return; }else{ 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 " + ShowLabel[i].value + ''; } //换行 excel += row + ""; //设置数据 for (var i = 0; i "; for (var index in arrData[i]) { var value = arrData[i][index].value === "." ? "" : arrData[i][index].value; row += '' + value + ''; } excel += row + ""; } excel += ""; var excelFile = ""; excelFile += ''; excelFile += ''; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += "{worksheet}"; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; 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); } ``` 原文地址: http://blog.csdn.net/sn_qmzm521/article/details/78775512
冀ICP备17029012号-4 | 版权所有©鲍亚龙 |免责声明  | GIF图库  | NUXT版