1 line
480 KiB
JavaScript
1 line
480 KiB
JavaScript
|
"undefined"==typeof layui&&"undefined"==typeof jQuery&&console.error("非layui调用请先加载jQuery"),"undefined"!=typeof jQuery&&($=jQuery),LAY_EXCEL={downloadExl:function(e,t,r){r=r||"xlsx",this.exportExcel({sheet1:e},t+"."+r,r,null)},exportExcel:function(e,t,r,n){r=r||"xlsx",t=t||"导出数据."+r;var a=XLSX.utils.book_new(),s={Title:t,Subject:"Export From web browser",Author:"excel.wj2015.com",Manager:"",Company:"",Category:"",Keywords:"",Comments:"",LastAuthor:"",CreatedData:new Date};n&&n.Props&&(s=$.extend(s,n.Props)),a.compression=!n||n.compression,!1!==a.compression&&(a.compression=!0),a.Props=s;var i,o,l,c,f,h,u={"!merges":null,"!margins":null,"!cols":null,"!rows":null,"!protect":null,"!autofilter":null};for(i in n&&n.extend&&(u=$.extend(u,n.extend)),u)u.hasOwnProperty(i)&&(u[i]||delete u[i]);for(o in $.isArray(e)&&(e={sheet1:e}),e)e.hasOwnProperty(o)&&(l=e[o],a.SheetNames.push(o),c=!1,l.length&&l[0]&&$.isArray(l[0])&&(c=!0),c?h=XLSX.utils.aoa_to_sheet(l):(c={},l.length&&(c.headers=l.unshift(),c.skipHeader=!0,f=this.splitContent(l)),h=XLSX.utils.json_to_sheet(l,c),u[o]?$.extend(h,u[o]):$.extend(h,u),void 0!==f&&this.mergeCellOpt(h,f.style)),a.Sheets[o]=h);r=XLSX.write(a,{bookType:r,type:"binary",cellStyles:!0,compression:a.compression});saveAs(new Blob([this.s2ab(r)],{type:"application/octet-stream"}),t)},splitContent:function(e){for(var t={},r=0;r<e.length;r++){var n,a,s=e[r],i=0;for(n in s)s.hasOwnProperty(n)&&("object"==typeof(a=s[n])?null!==a?t[this.numToTitle(i+1)+(parseInt(r)+1)]=a:s[n]="":(0===a&&(a={v:"0",s:{alignment:{horizontal:"right"}}}),t[this.numToTitle(i+1)+(parseInt(r)+1)]=a),i++)}return{content:e,style:t}},mergeCellOpt:function(e,t){for(var r in t)if(t.hasOwnProperty(r)){var n=t[r];if(e[r]){for(var a=["t","w","f","r","h","c","z","l","s"],s=0;s<a.length;s++)e[r][a[s]]=e[r][a[s]];$.extend(e[r],n)}}},tableToJson:function(e){e=$(e);var t=[];e.find("thead > tr").each(function(){var e=[];$(this).find("td,th").each(function(){e.push($(this).text())}),t.push(e)});var r=[];return e.find("tbody > tr").each(function(){var e=[];$(this).find("td").each(function(){e.push($(this).text())}),r.push(e)}),{head:t,body:r}},numsTitleCache:{},titleNumsCache:{},numToTitle:function(e){if(this.numsTitleCache[e])return this.numsTitleCache[e];var t="";if(26<e){var r=e%26,t=this.numToTitle((e-r)/26)+this.numToTitle(r||26);return this.numsTitleCache[e]=t,this.titleNumsCache[t]=e,t}return t=String.fromCharCode(64+e),this.numsTitleCache[e]=t,this.titleNumsCache[t]=e,t},titleToNum:function(e){if(this.titleNumsCache[e])return this.titleNumsCache[e];var t,r=e.length,n=0;for(t in e)e.hasOwnProperty(t)&&(n+=(e[t].charCodeAt()-64)*Math.pow(26,r-t-1));return this.numsTitleCache[n]=e,this.titleNumsCache[e]=n},setExportCellStyle:function(e,t,r,n){if("object"!=typeof e||!e.length||!e[0]||!Object.keys(e[0]).length)return[];var a=Object.keys(e[0]),s={c:0,r:0},i={c:e.length-1,r:a.length-1};t&&"string"==typeof t&&((t=t.split(":"))[0].length&&(s=this.splitPosition(t[0])),void 0!==t[1]&&""!==t[1]&&(i=this.splitPosition(t[1]))),s.c>i.c&&console.error("开始列不得大于结束列"),s.r>i.r&&console.error("开始行不得大于结束行");for(var o=s.r;o<=i.r;o++)for(var l=s.c;l<=i.c;l++){var c=e[o];if(!c){c={};for(var f=0;f<a.length;f++)c[a[f]]="";e[o]=c}var h=c[a[l]],u=null;null==h&&(h=""),u="object"==typeof h?$.extend(!0,{},h,r):$.extend(!0,{},{v:h},r),"function"==typeof n&&(u=n(h,u,c,r,o,l,a[l])),e[o][a[l]]=u}return e},makeMergeConfig:function(e){for(var t=[],r=0;r<e.length;r++)t.push({s:this.splitPosition(e[r][0]),e:this.splitPosition(e[r][1])});return t},makeColConfig:function(e,t){t=0<t?t:50;var r,n=[],a=0;for(r in e)if(e.hasOwnProperty(r)){var s=e[r];if(r.match&&r.match(/[A-Z]*/)){for(var i=this.titleToNum(r)-1;a<i;)n.push({wpx:t}),a++;a=1+i,n.push({wpx:0<s?s:t})}}return n},makeRowConfig:function(e,t){t=0<t?t:10;var r,n=[],a=0;for(r in e)if(e.hasOwnProperty(r)){var s=e[r];if(r.match&&r.match(/[0-9]*/)){for(var i=parseInt(r)-1;a<i;)n.push({hpx:t}),a++;a=1+i,n.push({hpx:0<s?s:t})}}return n},splitPosition:function(e){e=e.match("^([
|