Search code examples
javascriptxlsxalasql

Uncaught Error: Workbook is empty in xlsx and alasql excel file export


I'm using XLSX and alasql to export json object in to excel getting workbook is empty error, how to resolve

import * as alasql from 'alasql'
import * as XLSX from 'xlsx'

alasql.utils.isBrowserify = false
alasql.utils.global.XLSX = XLSX


const fileName = 'Ticket Details ' + props.ticketNumber
alasql('SELECT INTO XLSX("' + fileName + '.xlsx",?) FROM ?', [opts, finalData])

Solution

  • After debugging the code I got to know my finalData Array is empty, then I corrected the array value

    Solution:

    import * as alasql from 'alasql'
    import * as XLSX from 'xlsx'
    
    alasql.utils.isBrowserify = false
    alasql.utils.global.XLSX = XLSX
    
    const finalData = [
        {name:"John", city: "Seattle"},
        {name:"Mike", city: "Los Angeles"},
        {name:"Zach", city: "New York"}
    ]
    const fileName = 'Ticket Details ' + props.ticketNumber
    alasql('SELECT INTO XLSX("' + fileName + '.xlsx",?) FROM ?', [opts, finalData])