How to use xlsx-style to modify the exported Excel style
Excel is exported with borders, so that when printing, A4 paper can be spread as much as possible
Install XLSX – style
npm install xlsx-style --save
Copy the code
Modify xlsX-style source code resolve error
At \node_modules\xlsx-style\dist\cpexcel. Js var CPT = require('./ CPT '+ 'able'); Var CPT = cptable;Copy the code
Modify the code in vUE
import XLSX from "xlsx"; Import XLSXS from "xlsx-style" methods:{toExcel(){let wb = xlsx.utils.table_to_book (this.$refs.table,{sheet:' table '}) This.setexlstyle (wb['Sheets'][' Sheets']) // Merge cell border this.addrangeborder (WB ['Sheets'][' Sheets']][' Sheets']['! Merges'], wb [' Sheets'] [' grouped table ']) var ws = XLSXS. Write (wb, {type: "buffer"}); try { FileSaver.saveAs( new Blob([ws], { type: "application/octet-stream" }), `5.xlsx` ); } catch (e) { if (typeof console ! == "undefined") console.log(e, ws); } return ws; }, setExlStyle(data) {let borderAll = {top: {style: 'thin',}, bottom: {style: 'thin'}, left: {style: 'thin' }, right: { style: 'thin' } }; data['!cols'] = []; for (let key in data) { if (data[key] instanceof Object) { data[key].s = { border: borderAll, alignment: { horizontal: }, font:{sz:11}, bold:true, numFmt: 0} data['! Cols ']. Push ({WPX: 115}); } } return data; }, addRangeBorder(range,ws){ let arr = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]; range.forEach(item=>{ let startRowNumber = Number(item.s.c), endRowNumber = Number(item.e.c); for(let i = startRowNumber; i<= endRowNumber; i++){ for( let j = Number(item.s.r)+1; j<=Number(item.e.r); j++){ ws[`${arr[i]}${j+1}`] = {s:{border:{top:{style:'thin'}, left:{style:'thin'},bottom:{style:'thin'},right:{style:'thin'}}}}; } } }) return ws; }},Copy the code