利用js

申请删除 [复制链接]
查看16 | 回复0 | 2020-10-20 01:14:15 | |阅读模式
1、安装插件npminstallxlsx--savenpminstallfile-saver--save2、使用Export2Excel.js文件中/*eslint-disable*/import{saveAs}fromfile-saverimportXLSXfromxlsxfunctiongenerateArray(table){varout[];varrowstable.querySelectorAll(tr);varranges[];for(varR0;Rrows.length;R){varoutRow[];varrowrows[R];varcolumnsrow.querySelectorAll(td);for(varC0;Ccolumns.length;C){varcellcolumns[C];varcolspancell.getAttribute(colspan);varrowspancell.getAttribute(rowspan);varcellValuecell.innerText;if(cellValue!cellValuecellValue)cellValuecellValue;//Skiprangesranges.forEach(function(range){if(Rrange.s.rRrange.e.routRow.lengthrange.s.coutRow.lengthrange.e.c){for(vari0;irange.e.c-range.s.c;i)outRow.push(null);}});//HandleRowSpanif(rowspan||colspan){rowspanrowspan||1;colspancolspan||1;ranges.push({s:{r:R,c:outRow.length},e:{r:Rrowspan-1,c:outRow.lengthcolspan-1}});};//HandleValueoutRow.push(cellValue!?cellValue:null);//HandleColspanif(colspan)for(vark0;kcolspan-1;k)outRow.push(null);}out.push(outRow);}return[out,ranges];};functiondatenum(v,date1904){if(date1904)v1462;varepochDate.parse(v);return(epoch-newDate(Date.UTC(1899,11,30)))/(24*60*60*1000);}functionsheet_from_array_of_arrays(data,opts){varws{};varrange{s:{c:10000000,r:10000000},e:{c:0,r:0}};for(varR0;R!data.length;R){for(varC0;C!data[R].length;C){if(range.s.rR)range.s.rR;if(range.s.cC)range.s.cC;if(range.e.rR)range.e.rR;if(range.e.cC)range.e.cC;varcell{v:data[R][C]};if(cell.vnull)continue;varcell_refXLSX.utils.encode_cell({c:C,r:R});if(typeofcell.vnumber)cell.tn;elseif(typeofcell.vboolean)cell.tb;elseif(cell.vinstanceofDate){cell.tn;cell.zXLSX.SSF._table[14];cell.vdatenum(cell.v);}elsecell.ts;ws[cell_ref]cell;}}if(range.s.c10000000)ws[!ref]XLSX.utils.encode_range(range);returnws;}functionWorkbook(){if(!(thisinstanceofWorkbook))returnnewWorkbook();this.SheetNames[];this.Sheets{};}functions2ab(s){varbufnewArrayBuffer(s.length);varviewnewUint8Array(buf);for(vari0;i!s.length;i)view[i]s.charCodeAt(i)0xFF;returnbuf;}exportfunctionexport_table_to_excel(id){vartheTabledocument.getElementById(id);varoogenerateArray(theTable);varrangesoo[1];/*originaldata*/vardataoo[0];varws_nameSheetJS;varwbnewWorkbook(),wssheet_from_array_of_arrays(data);/*addrangestoworksheet*///ws[!cols][apple,bananws[!merges]ranges;/*addworksheettoworkbook*/wb.SheetNames.push(ws_name);wb.Sheets[ws_name]ws;varwboutXLSX.write(wb,{bookType:xlsx,bookSST:false,type:binary});saveAs(newBlob([s2ab(wbout)],{type:application/octet-stream}),test.xlsx)}//对此方法进行修改如下exportfunctionexport_json_to_excel({multiHeader2[],//第一行表头multiHeader[],//第二行表头header,//第三行表头在项目中没有使用到三级表头data,filename,//文件名merges[],//合并autoWidthtrue,bookTypexlsx}{}){/*originaldata*/filenamefilename||列表;data[...data]//data.unshift(header);for(letimultiHeader2.length-1;i-1;i--){data.unshift(multiHeader2[i])}for(letimultiHeader.length-1;i-1;i--){data.unshift(multiHeader[i])}varws_nameSheetJS;varwbnewWorkbook(),wssheet_from_array_of_arrays(data);if(merges.length0){if(!ws[!merges])ws[!merges][];merges.forEach(item{ws[!merges].push(XLSX.utils.decode_range(item))})}if(autoWidth){/*设置worksheet每列的最大宽度*/constcolWidthdata.map(rowrow.map(val{/*先判断是否为null/undefined*/if(valnull){return{wch:10};}return{wch:20};///*再判断是否为中文*///elseif(val.toString().charCodeAt(0)255){//return{//wch:val.toString().length*2//};//}else{//return{//wch:val.toString().length//};//}}))/*以第一行为初始值*/letresultcolWidth[0];for(leti1;icolWidth.length;i){for(letj0;jcolWidth[i].length;j){if(result[j][wch]colWidth[i][j][wch]){result[j][wch]colWidth[i][j][wch];}}}ws[!cols]result;}/*addworksheettoworkbook*/wb.SheetNames.push(ws_name);wb.Sheets[ws_name]ws;varwboutXLSX.write(wb,{bookType:bookType,bookSST:false,type:binary});saveAs(newBlob([s2ab(wbout)],{type:application/octet-stream}),${filename}.${bookType});}vue页面中使用//导出download(){//懒加载/utils/Export2Excel此路径为本地路径this.downloadLoadingtrueimport(/utils/Export2Excel).then((excel){constmultiHeader[[单据,,,,,,,,,,,,明细,,,,,,,]]constheader[[日期,单号,客户,大区,经手人,代码,名称,型号,编码,单位,数量,维修员,代码,名称,型号,单位,数量,性质]]constfilterVal[billDate,billNo,customer,region,makePeople,code,name,model,coded,unit,number,people,listCode,listName,listModel,listUnit,listNumber,type]//表头所对应的字段constobj{}for(constkeyinthis.conditions){if(key!pagekey!sizekey!sort){obj[key]this.conditions[key]}}console.log(obj,传递值)//去后台获取数据creatProcessExcel(obj).then(res{this.downloadLoadingfalseconstdatares.map(vfilterVal.map(jv[j]))//进行所有表头的单元格合并constmerges[A1:L1,M1:R1,A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2,L2,M2,N2,O2,P2,Q2,R2]excel.export_json_to_excel({multiHeader,//这里是第一行的表头header,//这里是第二行的表头data,filename:报表,merges})}).catch(err{this.downloadLoadingfalseconsole.log(err)})}})}点赞评论
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

网站更新