UNPKG

@liuyi_npm/excel

Version:

## 1.基础使用

1 lines 2.67 kB
!function(){var e={115:function(e){"use strict";e.exports=require("xlsx-js-style")},796:function(e){"use strict";e.exports=require("file-saver")}},s={};function t(r){var l=s[r];if(void 0!==l)return l.exports;var o=s[r]={exports:{}};return e[r](o,o.exports,t),o.exports}var r={};!function(){var e=r;const s=t(115),l=t(796),o=new class{readExcel(e){const t=s.readFile(e),r=t.SheetNames[0],l=t.Sheets[r];return s.utils.sheet_to_json(l)}exportExcel(e={}){const{header:t,data:r,filename:o,sheetName:n,autoWidth:a,watermark:c}={sheetName:"Sheet1",autoWidth:!0,watermark:"",...e},h=(e,s=0,t=0)=>{let r=t;const l={maxLevel:s,colProps:[],headerRows:[],merges:[],styles:{}};Array.isArray(e)||(e=[e]);for(const t of e)if(t.children&&t.children.length>0){const e=h(t.children,s+1,r);l.maxLevel=Math.max(l.maxLevel,e.maxLevel),l.colProps.push(...e.colProps),l.headerRows.push(...e.headerRows),l.merges.push(...e.merges),Object.assign(l.styles,e.styles),l.merges.push({s:{r:s,c:r},e:{r:s,c:r+e.colProps.length-1}}),t.style&&(l.styles[`${s},${r}`]=t.style),r+=e.colProps.length}else l.colProps.push(t.prop),s<l.maxLevel&&l.merges.push({s:{r:s,c:r},e:{r:l.maxLevel,c:r}}),t.style&&(l.styles[`${s},${r}`]=t.style),r++;return l},i=h(t),p=i.maxLevel+1,m=i.colProps,u=i.merges,g=i.styles,x=Array.from({length:p},(()=>Array.from({length:m.length},(()=>"")))),d=(e,s=0,t=0)=>{let r=t;Array.isArray(e)||(e=[e]);for(const t of e)t.children&&t.children.length>0?(x[s][r]=t.label,r+=d(t.children,s+1,r)):(x[s][r]=t.label,r++);return r-t};d(t);const f=r.map((e=>{const s=[];return m.forEach((t=>{s.push(e[t])})),s})),y=s.utils.book_new(),v=s.utils.aoa_to_sheet([...x,...f]);u.length>0&&(v["!merges"]||(v["!merges"]=[]),u.forEach((e=>{v["!merges"].push(e)})));for(let e=0;e<p;e++)for(let t=0;t<m.length;t++){const r=s.utils.encode_cell({r:e,c:t});v[r]||(v[r]={v:"",t:"s"}),v[r].s||(v[r].s={}),v[r].s.alignment={horizontal:"center",vertical:"center"};const l=`${e},${t}`;g[l]&&Object.assign(v[r].s,g[l])}if(a){const e=m.map(((e,s)=>({wch:Math.max(...[...x,...f].map((e=>{const t=e[s];if(null==t)return 8;const r=String(t),l=r.match(/[^\x00-\xff]/g)?.length||0,o=r.length-l;return Math.ceil(2.2*l+1.1*o)+1})))})));v["!cols"]=e}c&&(v["!merges"]||(v["!merges"]=[]),v["!merges"].push({s:{r:x.length+f.length+1,c:0},e:{r:x.length+f.length+1,c:m.length-1}}),s.utils.sheet_add_aoa(v,[[c]],{origin:-1})),s.utils.book_append_sheet(y,v,n);const b=s.write(y,{bookType:"xlsx",type:"array"}),w=new Blob([b],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});l.saveAs(w,`${o}.xlsx`)}},n=o.readExcel.bind(o),a=o.exportExcel.bind(o);e.readExcel=n,e.exportExcel=a}(),module.exports=r}();