javascriptexcelsheetjsjs-xlsx

writeFile returns empty file in SheetJs


I'm confused writing a .xlsx file using writeFile method because I'm getting an empty file.

The function receives a valid worksheet object(I inspected), the worksheet name display well but no information is in the file. Below is my code, thank you in advance.

function saveToExcel(event, worksheet) {
    const workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workbook, worksheet, "Lista de Evaporadores");
    event.sender.send('verga', workbook);
    XLSX.writeFile(workbook, `books/lista-evaporadores.xlsx`, {type: 'file'});
    open('books/lista-evaporadores.xlsx');
}

Solution

  • It's described pretty well here : https://github.com/SheetJS/js-xlsx/issues/817. This is the example of creating a worksheet and populating it with data, then adding that worksheet in a new workbook

    var workbook = xlsx.utils.book_new();
    var data = [
        {name:"John", city: "Seattle"},
        {name:"Mike", city: "Los Angeles"},
        {name:"Zach", city: "New York"}
    ];
    var ws = xlsx.utils.json_to_sheet(data);
    xlsx.utils.book_append_sheet(workbook, ws, "Results");
    xlsx.writeFile(workbook, 'out.xlsx', {type: 'file'});