Hi there,
After searching for few hours I can't find any answers to my questions so I'll try here. I'm using node 9.5, and the last XLSX version (0.12.0).
I can open the xlsx generated by the write (with binary string) while the writeFile works perfectly.
Here the smallest code snippet to reproduce the issue:
const book = XLSX.utils.book_new();
const sheet = XLSX.utils.aoa_to_sheet([
["a", "b", "c"],
[1, 2, 3]
]);
XLSX.utils.book_append_sheet(book, sheet, "test");
// Method 1 : failed to open the saved file
const content = XLSX.write(book, { type: 'binary', bookType: 'xlsx', bookSST: false });
fs.writeFileSync("/path/to/folder/test-write.xlsx", content);
// Method 2 : ok, the generated file works
XLSX.writeFile(book, "/path/to/folder/test-writeFile.xlsx");
What am I doing wrong?
It's my bad, I missed the { encoding: 'binary' }.
Here is the solution :
const content = XLSX.write(book, { type: 'binary', bookType: 'xlsx', bookSST: false });
fs.writeFileSync("/path/to/folder/test-write.xlsx", content, { encoding: 'binary' });
If you're using node, you can set type: 'buffer' to generate a Buffer:
const content = XLSX.write(book, { type: 'buffer', bookType: 'xlsx', bookSST: false });
fs.writeFileSync("/path/to/folder/test-write.xlsx", content);
Most helpful comment
If you're using node, you can set
type: 'buffer'to generate a Buffer: