import Excel from 'exceljs'; const arrayToSheet = (sheets, file_name) => { var workbook = new Excel.Workbook(); sheets.forEach(({ sheet_name, data, merges, styles }, index) => { var sheet = workbook.addWorksheet(sheet_name || `sheet_${index}`); sheet.addRows(data); if(merges) { merges.forEach(merge => { sheet.mergeCells(merge); }) } if(styles) { var styleMap = [ ['columns', 'column', 'getColumn'], ['rows', 'row', 'getRow'], ['cells', 'cell', 'getCell'] ]; styleMap.map(([k, i, a]) => { if(styles[k]) { styles[k].forEach(s => { Object.keys(s.style).map(key => { if(Array.isArray(s[i])) { if(i === 'cell') { s[i].forEach(c => { sheet[a](c)[key] = s.style[key]; }) } else { let [_s, _e] = s[i]; for(let _i = _s; _i <= _e; _i ++) { sheet[a](_i)[key] = s.style[key]; } } } else { sheet[a](s[i])[key] = s.style[key]; } }) }) } }) } }) workbook.xlsx.writeBuffer({ base64: true }) .then(buffer => { var blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = `${file_name}.xlsx`; a.click(); var dispose = () => URL.revokeObjectURL(blob); setTimeout(dispose, 100); }) }; export { arrayToSheet }