Powerful, lean and flexible csv-writer for node
npm install mg-csv-writer
npm install mg-csv-writer
`
Usage
`
var stockData = [{
Symbol: "AAPL",
Company: "Apple Inc.",
Price: 132.5,
TradeDt: new Date()
},
{
Symbol: "INTC",
Company: "Intel Corporation",
Price: 33.45,
TradeDt: new Date()
},
{
Symbol: "GOOG",
Company: "Google Inc",
Price: 554.52,
TradeDt: new Date()
},
{
Symbol: "TLSA",
Price: 554.52,
TradeDt: new Date()
}
];
const mgCsvWriter = require("mg-csv-writer");
const csvOut = new mgCsvWriter({
outfile: "/tmp/output.csv",
header: true,
quoted: false,
lineSep : "\n",
delimiter: ",",
columns: [{
id: "Symbol",
title: "Company Name",
fnFormat: function(colData, colId, rowData) {
// so something special!
return colData;
}
},
{ id: "Company",
title: "Id",
defaultValue: "no name" },
{ id: "Price",
title: "Price",
dataType: "int",
prefix: "$" },
{ id: "TradeDt",
title: "Trade Date",
dataType: "date",
dateMask: "yyyy-mm-dd"
}
]
});
csv.writeHeader();
csv.write(stockData); // can be a single row; or an array of rows
csv.write(stockData2); // multiple calls will add to the file
``