//报表导出 exportProjectCount: async function () { let that = this; await that.getProjectCount().then(() =>{ console.log( "日志输出",that.dataCount.length) let dataList = []; for (let i = 0; i < that.dataCount.length; i++) { let data = that.dataCount[i]; let obj = {}; obj['序号'] = i+1; if (data.hasOwnProperty('projectCountNum')) { obj['政府采购项目数(个)'] = data.projectCountNum; } if (data.hasOwnProperty('projectSmeCountNum')) { obj['专门面向中小微企业的项目数(个)'] = data.projectSmeCountNum; } if (data.hasOwnProperty('projectNoSmeCountNum')) { obj['非专门面向中小微企业的项目数(个)'] = data.projectNoSmeCountNum; } if (data.hasOwnProperty('programBudgetSum')) { obj['委托金额(万元)'] = data.programBudgetSum; } if (data.hasOwnProperty('offerPriceSum')) { obj['成交金额(万元)'] = data.offerPriceSum; } if (data.hasOwnProperty('savingCapital')) { obj['节约资金(万元)'] = data.savingCapital; } if (data.hasOwnProperty('savingRate')) { obj['节资率(%)'] = (data.savingRate*100).toFixed(2) + '%'; } dataList.push(obj); } //定义导出的格式类型 const opts = { bookType: 'xlsx', bookSST: false, type: 'binary' }; console.log('导出项目数据信息'); let sheetName = '导出项目数据信息'; let excelFileName = '项目数据统计'; const wb = { SheetNames: [sheetName], Sheets: {}, Props: {} }; wb.Sheets[sheetName] = XLSX.utils.json_to_sheet(dataList);//通过json_to_sheet转成单页(Sheet)数据 //定义表头快读,默认统一200 let cols = []; for (let i = 0; i < Object.keys(dataList[0]).length; i++) { let colWidth = { wpx: 200 } cols.push(colWidth); } wb.Sheets[sheetName]['!cols'] = cols; this.saveAs(new Blob([this.s2ab(XLSX.write(wb, opts))], { type: "application/octet-stream" }), excelFileName + '.' + (opts.bookType==="biff2"?"xls":opts.bookType)); }).catch(function (err) { console.log(err); return Promise.reject('导出失败'); }); }, saveAs: function(obj, fileName){ let tmp = document.createElement("a"); tmp.download = fileName || "下载"; tmp.href = URL.createObjectURL(obj); //绑定a标签 tmp.click(); //模拟点击实现下载 setTimeout(function () { //延时释放 URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL }, 100); }, s2ab: function(s){ if (typeof ArrayBuffer !== 'undefined') { let buf = new ArrayBuffer(s.length); let view = new Uint8Array(buf); for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } else { let buf = new Array(s.length); for (let i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF; return buf; } },
标签:obj,buf,Excel,前端,导出,length,let,hasOwnProperty,data From: https://www.cnblogs.com/Gourlay/p/18182781