日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

js JSON转Excel并导出

發布時間:2023/12/20 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js JSON转Excel并导出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近有JSON導出Excel的需求,查看了很多大神的例子。記錄一下

參考:https://blog.csdn.net/baidu_28665563/article/details/86225970

?

exportExcel.js代碼為:

export const exportExcel = (JSONData, FileName, title, filter) => {if (!JSONData) return;//轉化json為objectvar arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;var excel = "<table>";//設置表頭var row = "<tr>";if (title) { //使用標題項for (var i in title) {if (typeof (title[i]) == 'string') {row += "<th align='center'>" + title[i] + "</th>";}}} else { //不使用標題項for (var i in arrData[0]) {row += "<th align='center'>" + i + "</th>";}}excel += row + "</tr>";//設置數據for (var i = 0; i < arrData.length; i++) {var row = "<tr>";for (var index in arrData[i]) {//判斷是否有過濾行if (filter) {if (filter.indexOf(index) == -1) {var value = arrData[i][index] == null ? "" : arrData[i][index];row += "<td>" + value + "</td>";}} else {var value = arrData[i][index] == null ? "" : arrData[i][index];row += "<td align='center' style='width: 90px;'>" + value + "</td>";}}excel += row + "</tr>";}excel += "</table>";var excelFile ="<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";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 += "<head>";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 += "</head>";excelFile += "<body>";excelFile += excel;excelFile += "</body>";excelFile += "</html>";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); }

上述代碼是略微修改后的代碼。修改部分為:

if (title) { //使用標題項for (var i in title) {if (typeof (title[i]) == 'string') {row += "<th align='center'>" + title[i] + "</th>";}} } else { //不使用標題項for (var i in arrData[0]) {row += "<th align='center'>" + i + "</th>";} }

因為如果傳入title后,在遍歷的時候會多遍歷出來三個函數在表頭后面。所以加了一個篩選條件。

?

調用方法:

let title = ['序號', '宿舍樓', '樓層', '宿舍', '姓名', '班級', '人臉信息', '說明']; let fileName = '附錄:未' + this.schedulesType + '學生名單'; let JSONData = JSON.stringify(this.downloadData)exportExcel(JSONData, fileName, title);

其中title為表格header ? filename為表格名字 ?JSONData為表格內數據。 ?還接受一個參數filter為過濾項。

總結

以上是生活随笔為你收集整理的js JSON转Excel并导出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。