<u id="g9yg5"></u>

<video id="g9yg5"></video>

<source id="g9yg5"></source>
<b id="g9yg5"><acronym id="g9yg5"><bdo id="g9yg5"></bdo></acronym></b>

    1. <source id="g9yg5"><track id="g9yg5"><ins id="g9yg5"></ins></track></source>

      如何利用node生成word文檔(node,Word,web開發)

      時間:2024-04-23 05:53:18 作者 : 石家莊SEO 分類 : web開發
      • TAG :

      這篇“如何利用node生成word文檔”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何利用node生成word文檔”文章吧。

      如何利用node生成word文檔

      依賴

      //https://docx.js.org/#/npmidocx//https://www.npmjs.com/package/downloadnpmidownload

      說明,因為docx繪圖只支持文件流,所以要把網絡文件下載到本地轉成buffer

      代碼

      話不多說,上代碼

      import*asfsfrom"fs"import{Document,Packer,Paragraph,TextRun,ImageRun,HeadingLevel,AlignmentType,convertInchesToTwip,Table,TableRow,TableCell,WidthType,VerticalAlign,BorderStyle}from"docx"constdownload=require('download')//性別enumGender{Male='male',Female='female'}//選手typePlayerSchema={name:stringgender:stringidCard?:stringbirthday?:stringweight?:stringremark?:stringavatar?:stringlocalAvatar?:stringlevel:string}typeGroupSchema={//gender:Genderinstitution:stringleader:stringphone:stringcoach:stringdoctor:stringplayers:PlayerSchema[]}//所有數據interfaceDataSchema{[key:string]:GroupSchema}//表格無邊框constnoBoder={top:{style:BorderStyle.NIL,size:0,color:'FFFFFF'},bottom:{style:BorderStyle.NIL,size:0,color:'FFFFFF'},left:{style:BorderStyle.NIL,size:0,color:'FFFFFF'},right:{style:BorderStyle.NIL,size:0,color:'FFFFFF'}}//刪除下載的照片及文件夾functiondelStaticFile(groupNames:string[]){for(letgroupNameofgroupNames){if(fs.existsSync(groupName)){constfiles=fs.readdirSync(groupName)files.map((file:string)=>{letcurPath=groupName+"/"+file//刪除選手招聘fs.unlinkSync(curPath)})fs.rmdirSync(groupName)}}}//生成wordasyncfunctiongenerate(data:DataSchema){constgroupNames=Object.keys(data)//比較粗糙的控制單元格長度邏輯constlongHeaders=['身份證號','備注']//下載遠程資源到本地for(letgroupNameofgroupNames){if(!fs.existsSync(groupName)){fs.mkdirSync(groupName)}constplayers=data[groupName].playersfor(letplayerofplayers){if(player.avatar){constavatarArr=player.avatar.split('/')constfileName=`${groupName}/${avatarArr[avatarArr.length-1]}`if(!fs.existsSync(fileName)){awaitdownload(player.avatar,groupName)}//下載后的本地的資源路徑player.localAvatar=fileName}}}//需要多個文件合一constsections=groupNames.map(groupName=>{constinfo=data[groupName]const{institution,leader,phone,coach,doctor,players}=info//標頭內容//letheaders=['序號','照片','姓名','性別','出生年月','體重','級別','備注']letheaders=['序號','照片','姓名','性別','身份證號','級別','備注']//表格數據lettableData:any[][]=[]tableData.push(headers)//填充選手信息letindex=1for(letplayerofplayers){tableData.push([index.toString(),player.localAvatar||'',player.name,player.gender===Gender.Male?'男':'女',player.idCard,//player.birthday,//player.weight,player.level,player.remark,])index++}//表格渲染consttableRows=tableData.map(colums=>{returnnewTableRow({children:colums.map(cell=>{returnnewTableCell({verticalAlign:VerticalAlign.CENTER,width:{//設置寬度dxa長度單位https://stackoverflow.com/questions/14360183/default-wordml-unit-measurement-pixel-or-point-or-inchessize:longHeaders.some(j=>cell===j)?3000:800,type:WidthType.DXA,},children:cell&&colums.findIndex(i=>i===cell)===1&&cell!=='照片'?[newParagraph({alignment:AlignmentType.CENTER,children:[newImageRun({//將圖片轉化為bufferdata:fs.readFileSync(cell),transformation:{width:100,height:129,},})]})]:[newParagraph({alignment:AlignmentType.CENTER,children:[newTextRun(cell||'')]})]})})})})//渲染報名表格consttable=newTable({alignment:AlignmentType.CENTER,rows:tableRows})return{properties:{},children:[//newParagraph({//style:"wellSpaced",//children:[//newTextRun({//text:'附件4',//color:'999999',//})//],//}),//表頭信息newParagraph({spacing:{before:400,after:400},style:"Title",text:`自由搏擊比賽報名表(${groupName===Gender.Male?'男子':'女子'})`,heading:HeadingLevel.TITLE,alignment:AlignmentType.CENTER}),//隊伍信息newTable({style:"wellSpaced",alignment:AlignmentType.CENTER,borders:noBoder,rows:[newTableRow({children:[newTableCell({width:{size:600,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`單位:`),],}),newTableCell({width:{size:1800,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`${institution}`)],}),newTableCell({width:{size:700,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`領隊:`),],}),newTableCell({width:{size:1200,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`${leader}`)],}),newTableCell({width:{size:1100,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`聯系電話:`),],}),newTableCell({width:{size:1400,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`${phone}`)],}),newTableCell({width:{size:700,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`教練:`),],}),newTableCell({width:{size:1300,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`${coach}`)],}),newTableCell({width:{size:700,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`隊醫:`),],}),newTableCell({width:{size:1300,type:WidthType.DXA,},borders:noBoder,children:[newParagraph(`${doctor}`)],}),],}),]}),//用于段落距離(table無法設置spacing屬性)newParagraph({spacing:{//通過調整before值來調整段落漸進before:400,},text:``,}),//選手信息table,//印章和時間newParagraph({style:"wellSpaced",children:[newTextRun({text:'\t\t\t\t報名單位章:\t\t\t\t\t\t',}),newTextRun({text:'年\t\t'}),newTextRun({text:'月\t\t'}),newTextRun({text:'日'})]})]}})//創建整個文檔constdoc=newDocument({styles:{paragraphStyles:[{id:"Title",name:"title",basedOn:"Normal",next:"Normal",quickFormat:true,run:{size:30,bold:true,color:"000000"}},{id:"wellSpaced",name:"WellSpaced",basedOn:"Normal",quickFormat:true,paragraph:{indent:{left:convertInchesToTwip(0.5),},spacing:{before:400,},},},],},sections})//生成word文檔Packer.toBuffer(doc).then((buffer)=>{fs.writeFileSync("enrolls.docx",buffer)})//刪除下載的選手照片delStaticFile(groupNames)}constgroup:GroupSchema={institution:'江蘇省南京市舜禹集團總部',leader:'王猛(男)',phone:'18861856665',coach:'劉國梁(男)',doctor:'楊永信(女)',players:[{name:'萊昂納多迪卡普里奧',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/13.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/7.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',idCard:'320888199001019878',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'},{name:'張三',gender:Gender.Male,idCard:'320888199001019878',birthday:'1999-01-02',weight:'60kg',avatar:'https://multi-xm.oss-cn-hangzhou.aliyuncs.com/atms/14.png',remark:'',level:'60kg'}]}constdata:DataSchema={[Gender.Male]:group,[Gender.Female]:group,}generate(data)

      以上就是關于“如何利用node生成word文檔”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

      本文:如何利用node生成word文檔的詳細內容,希望對您有所幫助,信息來源于網絡。
      上一篇:如何使用CSS濾鏡實現圓角及波浪效果下一篇:

      1 人圍觀 / 0 條評論 ↓快速評論↓

      (必須)

      (必須,保密)

      阿貍1 阿貍2 阿貍3 阿貍4 阿貍5 阿貍6 阿貍7 阿貍8 阿貍9 阿貍10 阿貍11 阿貍12 阿貍13 阿貍14 阿貍15 阿貍16 阿貍17 阿貍18
      中文字幕第63久久_欧美乱人伦视频中文字幕_337p大胆精品_午夜亚洲福利在线老司机

      <u id="g9yg5"></u>

      <video id="g9yg5"></video>

      <source id="g9yg5"></source>
      <b id="g9yg5"><acronym id="g9yg5"><bdo id="g9yg5"></bdo></acronym></b>

      1. <source id="g9yg5"><track id="g9yg5"><ins id="g9yg5"></ins></track></source>