Export Excel files client-side with JavaScript
Being able to export spreadsheet files such as .xls, .csv, .xlsx, etc, on the client is a pretty cool feature and can be easily implemented using JavaScript.
To achieve such a task, I'll use an approach that requires installing the following libraries:
file-saver: Solution to saving files on the client-side.
xlsx: Parser and writer for various spreadsheet formats.
npm install file-saver xlsx
Create a function to receive the desired data to be written to a spreadsheet. I named it exportSpreadsheetFile.js and it will generate a .xlxs file.
This approach uses the Array of Arrays method, which I find pretty straight forward:
The function takes an array object and each array inside of it will become a row. To illustrate, here's an example:
That's it, now you just have to import and use the exportToSpreadsheet function created, passing the desired data and the file name.