Ejemplo 1: crear un archivo csv nodejs
const csv = require('csv-parser');
const fs = require('fs');
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (row) => {
console.log(row);
})
.on('end', () => {
console.log('CSV file successfully processed');
});
Ejemplo 2: crear un archivo csv nodejs
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
path: 'out.csv',
header: [
{id: 'name', title: 'Name'},
{id: 'surname', title: 'Surname'},
{id: 'age', title: 'Age'},
{id: 'gender', title: 'Gender'},
]
});
const data = [
{
name: 'John',
surname: 'Snow',
age: 26,
gender: 'M'
}, {
name: 'Clair',
surname: 'White',
age: 33,
gender: 'F',
}, {
name: 'Fancy',
surname: 'Brown',
age: 78,
gender: 'F'
}
];
csvWriter
.writeRecords(data)
.then(()=> console.log('The CSV file was written successfully'));
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)