I am a nodejs programmer . Now I have a table of data that I want to save in Excel File format . How do I go about doing this ?
I found a few Node libraries . But most of them are Excel Parsers rather than Excel Writers .I am using a Linux Server . Hence need something that can run on Linux . Please let me know if there are any helpful libraries that you know of .
Or is there a way I can convert a CSV file to an xls file ( programmatically ) ?
XLSx in the new Office is just a zipped collection of XML and other files. So you could generate that and zip it accordingly.
Bonus: you can create a very nice template with styles and so on:
content.xml
(orxl/worksheets/sheet1.xml
) with your dataHowever I found ODS (openoffice) much more approachable (excel can still open it), here is what I found in
content.xml
You should check ExcelJS
Works with CSV and XLSX formats.
Great for reading/writing XLSX streams. I've used it to stream an XLSX download to an Express response object, basically like this:
Works great for large files, performs much better than excel4node (got huge memory usage & Node process "out of memory" crash after nearly 5 minutes for a file containing 4 million cells in 20 sheets) since its streaming capabilities are much more limited (does not allows to "commit()" data to retrieve chunks as soon as they can be generated)
See also this SO answer.
Or - build on @Jamaica Geek's answer, using Express - to avoid saving and reading a file:
Use msexcel-builder. Install it with:
Then:
Using fs package we can create excel/CSV file from JSON data.
Step 1: Store JSON data in a variable (here it is in jsn variable).
Step 2: Create empty string variable(here it is data).
Step 3: Append every property of jsn to string variable data, while appending put
'\t'
in-between 2 cells and '\n' after completing the row.Code:
I just figured a simple way out . This works -
Just create a file with Tabs as delimiters ( similar to CSV but replace comma with Tab ). Save it with extension .XLS . The file can be opened in Excel .
Some code to help --
This creates the file in XLS file format . It doesnt work if you try XLSX instead of XLS .