Sheetjs: How to convert api json response to .xlsx file and then download?

Created on 10 Oct 2017  路  3Comments  路  Source: SheetJS/sheetjs

Since there is so much going in the documentation I can't seem to figure out a straightforward way of implementing this feature in my node-react app:

The API for my project is serving me array of objects containing data in json format page wise. Means, to get the entire data I have to make recursive get calls to the api like this: request.get(/contacts/page/(1...blank response page) and keep on concatenating data in a variable.

Now, I want to be able to convert this json data stored in a variable to a .xlsx file and make it available as a download or download concurrently as the data is fetching from the api.

I just need help in figuring out the steps for this process and nothing else. Thanks!

Most helpful comment

Supposing that data is an array of JS objects (request, JSON.parse, append to an array), you need to build the workbook then generate a download. The json_to_sheet utility does the hard part:

/* make the worksheet */
var ws = XLSX.utils.json_to_sheet(data);

/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");

/* write workbook */
var buf = XLSX.write(wb, {bookType:'xlsx', type:'buffer'}); // generate a nodejs buffer
var str = XLSX.write(wb, {bookType:'xlsx', type:'binary'}); // generate a binary string in web browser

See the discussion in https://github.com/SheetJS/js-xlsx/issues/817 for longer explanation as well as a sample jsfiddle and a sample runkit

All 3 comments

Supposing that data is an array of JS objects (request, JSON.parse, append to an array), you need to build the workbook then generate a download. The json_to_sheet utility does the hard part:

/* make the worksheet */
var ws = XLSX.utils.json_to_sheet(data);

/* add to workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "People");

/* write workbook */
var buf = XLSX.write(wb, {bookType:'xlsx', type:'buffer'}); // generate a nodejs buffer
var str = XLSX.write(wb, {bookType:'xlsx', type:'binary'}); // generate a binary string in web browser

See the discussion in https://github.com/SheetJS/js-xlsx/issues/817 for longer explanation as well as a sample jsfiddle and a sample runkit

I am able to work on it and it is downloadable but my requirement is to put some title on top as well .
Requirement is like following format : -
Please have a look on this ,
selection_106

How can i achieve that ? I am getting only data set not title ,

The file which is downloaded from js code using node-excel-export npm is not readable using xlsx-to-json npm .how can i read sheetjs file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eyalcohen4 picture eyalcohen4  路  3Comments

Alex0007 picture Alex0007  路  3Comments

dullin picture dullin  路  3Comments

nishthasb picture nishthasb  路  4Comments

DannyRyman picture DannyRyman  路  3Comments