Search code examples
javascriptnode.jsexceldownloadxlsx

Node.js: Created xlsx file that's stored in a variable, but uncertain how to save it


I'm a newbie at Node.js, and I was trying out a package xlr that supposedly allows me to export a data set to an Excel xlsx file.

I've completed all the steps to the point where I have an xlsx file stored in a variable, and I'm confident that all the steps that I've done up to that point are correct.

In the readme, that would be when:

const result = xlr(conf);

My question is: How would I save that file?

I've already tried writing the variable to a file, which didn't work, and the example provided by the xlr github readme uses Express, whereas I'm using the Node.js terminal to try to directly save it.

This is my first time doing anything with Node.js, so please forgive me if this is a dumb question, but I've spent a few hours trying to troubleshoot it and I haven't found any information relevant to it.

Thank you.


Solution

  • Since you want to avoid using http/express, you can save the file instead with

    fs.writeFile("temp.xlsx", new Buffer(result, 'binary'), err => {
        if (err) {
            console.error(err);
        } else {
            console.log("File written");
        }
    });