Skip to content

Instantly share code, notes, and snippets.

@akatreyt
Created May 13, 2014 04:17
Show Gist options
  • Save akatreyt/d191fb98d8efabc08770 to your computer and use it in GitHub Desktop.
Save akatreyt/d191fb98d8efabc08770 to your computer and use it in GitHub Desktop.
/**
* ExcelControllerController.js
*
* @description ::
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var csv = require('json-csv')
var fs = require('fs');
var http = require('http');
module.exports = {
createTest: function(req, res) {
var fields = req.param('fields');
console.log(fields);
var items = [{
name: 'fred',
email: 'fred@somewhere',
amount: 1.02
}, {
name: 'jo',
email: 'jo@somewhere',
amount: 1.02
}, {
name: 'jo with a comma,',
email: 'jo@somewhere',
amount: 1.02
}, {
name: 'jo with a quote"',
email: 'jo@somewhere',
amount: 1.02
}]
csv.toCSV({
data: items,
fields: [{
name: 'name',
label: 'Name',
quoted: true
}, {
name: 'email',
label: 'Email'
}, {
name: 'amount',
label: 'Amount'
}]
},
function(err, csv) {
var cd = process.cwd();
cd = cd + "/.tmp/public/linker/files/data.csv"
console.log(cd);
fs.writeFile(cd, csv, function(err) {
if (err) {
console.log(err);
} else {
console.log("The file was saved!");
res.download(cd, req.param('file'), function(err) {
console.log("The file was deleted!");
fs.unlink(cd);
});
}
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment