1. xlsx 설치
$ npm install xlsx
2. json 형식 excel data set
let excelDataList = [
{ name: 'A', count: 10, note: 'note' },
{ name: 'B', count: 20, note: 'note' },
{ name: 'C', count: 30, note: 'note' },
{ name: 'D', count: 40, note: 'note' }
]
excelDataList.map((el) => {
el['이름'] = el.name
el['횟수'] = el.count
delete el.name
delete el.count
delete el.note
return el
})
2-1. 합계행 추가
let totalRowData = {}
totalRowData['이름'] = '합계'
totalRowData['횟수'] = excelDataList.reduce((acc, el) => acc + el['횟수'], 0)
excelDataList.push(totalRowData)
3. excel download
const excelData = XLSX.utils.json_to_sheet(excelDataList)
const workBook = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(workBook, excelData, 'sheet1')
XLSX.writeFile(workBook, '엑셀 파일 다운로드.xlsx')