Home > database >  How can I write xlsx to file in browser with ExcelJS?
How can I write xlsx to file in browser with ExcelJS?

Time:12-04

I am trying to create an xlsx file in the browser and find https://github.com/exceljs/exceljs very powerful. However, I can't find a way how to save my xlsx object to a file. Probably I need to use Buffer, but how to generate a file from it?

const buffer = await workbook.xlsx.writeBuffer();

This library can generate files in the browser https://docs.sheetjs.com/docs/ but it is not good at building complex fields.

CodePudding user response:

To save the xlsx file in the browser with ExcelJS, you can use the saveAs method. This method allows you to save a file from a JavaScript program in the browser.

Here is an example:

const workbook = new Excel.Workbook();

// Add data to the workbook

const buffer = await workbook.xlsx.writeBuffer();

const fileSaver = require('file-saver');

fileSaver.saveAs(new Blob([buffer]), 'my-file.xlsx');

CodePudding user response:

To generate a file from a Buffer object using the exceljs library, you can use the saveAs method provided by the FileSaver.js library. Here's an example of how you can use it:

const buffer = await workbook.xlsx.writeBuffer();

// Import the FileSaver.js library
import * as FileSaver from 'file-saver';

// Use the FileSaver.js saveAs() method to save the file
FileSaver.saveAs(new Blob([buffer]), 'my-file.xlsx');

The saveAs method will trigger a download prompt in the browser, allowing the user to save the generated file to their local system.

  • Related