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
2 Answers
Reset to default 3File 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
版权声明:本文标题:javascript - How to save csv data into .csv file in nodejs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744669998a2618768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论