Xlsx-js download file - opinion you
SheetJS Tutorial Create xlsx with Javascript
In this tutorial, were going to talk about SheetJs. An Excel JavaScript library that let you do so many things with Excel such as from creating exporting workbook from scratch, converting html table, array or JSON into downloadable xlsx file. And the best thing is that, everything will be done on browser-side only. No server-side script or AJAX involved at all!
There 2 versions of SheetJS, one is community version which is free, and Pro version which has extended feature. Obviously, were going to use community version for this tutorial. Just download from github and copy the taniaarraindegia.esy.es to your web directory and include it to your script tag
Creating a Workbook
Now lets start with creating a new workbook by calling book_new() utility function which will return an empty workbook object.
var wb = taniaarraindegia.esy.es_new();You can update the workbook properties such as title, subject, author with taniaarraindegia.esy.es
taniaarraindegia.esy.es = { Title: "SheetJS Tutorial", Subject: "Test", Author: "Red Stapler", CreatedDate: new Date(,12,19) };Now we have the workbook, the next step is to create a worksheet and add it to the workbook. First, youll need to assign a new sheet name and push it to the SheetNames array.
taniaarraindegia.esy.es("Test Sheet");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, Im going to use array of array. The structure is quite straightforward. Each array represent the row data and the members are the cell content.
var ws_data = [['hello' , 'world']]; //a row with 2 columnsNow create the sheet from this array by using aoa_to_sheet()
var ws = taniaarraindegia.esy.es_to_sheet(ws_data);And assign the sheet object to the workbook Sheets array.
taniaarraindegia.esy.es["Test Sheet"] = ws;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
var wbout = taniaarraindegia.esy.es(wb, {bookType:'xlsx', type: 'binary'});We now have our xlsx binary data on wbout var. However, the correct content type for excel file is octet stream so youll need to convert the binary data into octet. We can achieve that by using arrayBuffer, UInt8Array and bit operation like this.
function s2ab(s) { var buf = new ArrayBuffer(taniaarraindegia.esy.es); //convert s to arrayBuffer var view = new Uint8Array(buf); //create uint8array as viewer for (var i=0; i<taniaarraindegia.esy.es; i++) view[i] = taniaarraindegia.esy.esdeAt(i) & 0xFF; //convert to octet return buf; }Were going to utilize taniaarraindegia.esy.es 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.
Were ready; Lets test it!
You can see it in action in video below.
So thats the basic of how to create Excel file with pure javascript using SheetJS. In next tutorial, well show you how to convert HTML table into excel file in just a few minutes with SheetJS.
Check our other interesting tutorial from our Youtube Channel Dont forget to like or subscribe to stay tune 🙂
Source Code

-
-