Reputation: 1821
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');
}
Upvotes: 2
Views: 6419
Reputation: 20078
When ever your data Object is empty you will receive the error mentioned above
Error:
var workbook = xlsx.utils.book_new();
var data = [];
var ws = xlsx.utils.json_to_sheet(data); // data Array is empty
xlsx.utils.book_append_sheet(workbook, ws, "Results");
xlsx.writeFile(workbook, 'out.xlsx', {type: 'file'});
Solution:
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); // data Array not empty
xlsx.utils.book_append_sheet(workbook, ws, "Results");
xlsx.writeFile(workbook, 'out.xlsx', {type: 'file'});
Upvotes: 0
Reputation: 96
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'});
Upvotes: 6