Sheetjs: Write vs WriteFile

Created on 14 Feb 2018  路  2Comments  路  Source: SheetJS/sheetjs

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?

Most helpful comment

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);

All 2 comments

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);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

thomasledoux1 picture thomasledoux1  路  3Comments

gustavosimil picture gustavosimil  路  3Comments

HachimDev picture HachimDev  路  3Comments

goxr3plus picture goxr3plus  路  3Comments

eyalcohen4 picture eyalcohen4  路  3Comments