DEV Community

Matheus Costa
Matheus Costa

Posted on • Updated on

Simple JSON to Excel in JavaScript - Code Snippet

const ExcelJS = require('exceljs');

const createWorkbook = async (worksheetName, jsonData) => {
  const workbook = new ExcelJS.Workbook()
  const worksheet = workbook.addWorksheet(worksheetName)

  const keys = [...new Set(jsonData.flatMap(item => {
    return Object.keys(item)
  }))]

  const values = jsonData.map(item => {
    return keys.map(key => {
      if(!item[key]) {
        return ''
      }

      return item[key]
    })
  })

  worksheet.columns = keys.map(entry => {
    return {
      header: entry,
      key: entry,
      width: 24
    }
  })

  values.forEach(value => worksheet.addRow(value))

  return workbook
}
Enter fullscreen mode Exit fullscreen mode

Or use my npm package json-to-excel

Top comments (0)