CSV or XSLX export on client requires a lot of memory, it must uses BLOB(Binary large object) to avoid browser crash.
Blob means “Binary Large Object” and it’s an opaque representation of a chunk of bytes. Web Browsers implement a
Blob object, which is responsible for holding data.
BLOB size and implementation is different via browser :
join() method returns the array as a string.
The elements will be separated by a specified separator. The default separator is comma (,).
Export array to xlsx is more diffcult(convert string to utf-16 binary needed), can be done using sheetjs plugin as done below:
Creating a Workbook
Now let’s start with creating a new workbook by calling book_new() utility function which will return an empty workbook object.
You can update the workbook properties such as title, subject, author with wb.Props.
Now we have the workbook, the next step is to create a worksheet and add it to the workbook. First, you’ll need to assign a new sheet name and push it to the SheetNames array.
Then, for the content inside the sheet, you have several options. You have create a sheet from array of array, JSON or html table. For this tutorial, I’m going to use array of array. The structure is quite straightforward. Each array represent the row data and the members are the cell content.
Now create the sheet from this array by using aoa_to_sheet()
And assign the sheet object to the workbook Sheets array.
Congratulation, now you have created a workbook and a worksheet with first row of data. The next step is to generate an xlsx file.
Exporting Workbook for Download
We need to export the workbook as xlsx binary. Use write function then pass the bookType as xlsx and output Type as binary
We now have our xlsx binary data on wbout var. However, the correct content type for excel file is octet stream so you’ll need to convert the binary data into octet. We can achieve that by using arrayBuffer, UInt8Array and bit operation like this.
We’re going to utilize Filesaver.js and Blob to handle the file saving for cross browser support. Use saveAs() function and create a new Blob object from octet array. Set the content type as octet-stream. follow by excel file naming that you would like.
We’re ready; Let’s test it!