标签:nodejs
Nodejs实现CSV格式的数据导出
使用的第三方包:
fast-csv: npm install fast-csv
async: npm install async
mysql: npm install mysql
在这个实现中使用mysql数据源将mysql中的数据导入到对应的csv文件中。
/** * 实现dump数据到csv文件数据中 * 导出csv数据模版 **/ var config = require('./info.json'); var fs = require("fs"); var csv = require('fast-csv'); var async = require("async"); var mysql = require('mysql'); var connection = mysql.createConnection({ host : config['mysql']['host'], user : config['mysql']['user'], password : config['mysql']['pwd'], database : config['mysql']['db'] }); //用于判断是否是最后一个数据信息 var endLine = false; var stream = null; var paramArgus = process.argv.splice(2); if(!paramArgus || paramArgus.length == 0){ stream = fs.createWriteStream("new.csv"); }else{ stream = fs.createWriteStream(paramArgus[0]); } stream.on("finish", function(){ console.log("dump to csv ok!"); }); //生成头部 var csvStream = csv.format({headers: true}) .transform(function(row){ return { 编号: row.id, 需求名称: row.name }; }); //获取数据信息接口 function searchData(){ csvStream.pipe(stream); connection.query("select id,name from snz_requirements" , function(err , results, callback){ // console.log(results); // csv.write(results, {headers: true}).pipe(stream); results.forEach(function(row){ csvStream.write({id:row['id'], name: row['name']}); }); //关闭写入 csvStream.end(); connection.end(); }); } function asyncToCSV(){ async.auto({ createData: function(callDo){ connection.query("select id,name from snz_requirements" , function(err , results, callback){ if(callDo) callDo(null , results); }); }, //依次执行的处理 findData: ['createData' , function(callDo , value){ var reqValue = value['createData']; async.map(reqValue, function(row, callback) { connection.query("select count(*) as countV from snz_modules where requirement_id=?", row["id"] , function(err , results, callbackDeal){ row["count"] = results[0].countV; if(callback) callback(null , row); }); }, function(err,results) { //当整个map中的数据都执行完成之后才会执行下一个队列中的function if(callDo) callDo(null , results); }); }], showDetail: ['findData' , function(callDo , value){ //通过csv写入到输出流中保存到csv文件 csv.write(value['findData'], {headers: true}).pipe(stream); connection.end(); if(callDo) callDo(null , "end!"); }] }, function(err , value){ // console.log(value); }); } asyncToCSV();
标签:nodejs
原文地址:http://blog.csdn.net/michaelzhaozero/article/details/41692729