admin管理员组

文章数量:1391955

This is my "app.js" file.

const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const val = convertArrayToCSV(dataArrays, {
header,
separator: ','
});
console.log(val)

output:

number,first,last,handle
1,Mark,Otto,@mdo  
2,Jacob,Thornton,@fat
3,Larry,the Bird,@twitter

I want to save this "Val" as a .csv file on my device. How can I do that? Please help. I am stuck here for the last 4 hours.

This is my "app.js" file.

const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const val = convertArrayToCSV(dataArrays, {
header,
separator: ','
});
console.log(val)

output:

number,first,last,handle
1,Mark,Otto,@mdo  
2,Jacob,Thornton,@fat
3,Larry,the Bird,@twitter

I want to save this "Val" as a .csv file on my device. How can I do that? Please help. I am stuck here for the last 4 hours.

Share Improve this question asked Jan 13, 2021 at 14:09 Mujahidul IslamMujahidul Islam 5638 silver badges10 bronze badges 4
  • filesystem fs library and there the function writeFile – Aalexander Commented Jan 13, 2021 at 14:11
  • I use writefile function but failed. – Mujahidul Islam Commented Jan 13, 2021 at 14:19
  • @Mujahidaul Islam as your convertArrayToCSV results in an string it should work – Aalexander Commented Jan 13, 2021 at 14:22
  • it could be that you need write access to the directory where you will create the file. You can check this in unix enviroment with ls -al Or do as path your home folder /home/newcsvfile.csv ~/newcsvfile.csv – Aalexander Commented Jan 13, 2021 at 14:24
Add a ment  | 

2 Answers 2

Reset to default 3

File System Library

To achieve this you can use the File System Library of NodeJs

Data can be: <string> | <Buffer> | <TypedArray> | <DataView> | <Object>

As option you can parse an encoding like

fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);


const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const fs = require('fs')
const csv = require('csv-parser')
const randomWords = require('random-words')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const val = convertArrayToCSV(dataArrays, {
header,
separator: ','
});
console.log(val)


fs.writeFile('<pathtodirectory>/message.csv', val, (err) => {
      if (err) throw err;
      console.log('The file has been saved!');
    });    
const fs = require('fs')
const header = ['number', 'first', 'last', 'handle'];

const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];

const val = [header].concat(dataArrays).map(arr => arr.join(',')).join('\r\n');

fs.writeFile('filename.csv', val, err => {
  if(err) console.error(err);
  else console.log('Ok');
}););

本文标签: javascriptHow to save csv data into csv file in nodejsStack Overflow