Skip to content

Instantly share code, notes, and snippets.

@RandomEtc
Created February 11, 2012 19:06
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save RandomEtc/1803645 to your computer and use it in GitHub Desktop.
Save RandomEtc/1803645 to your computer and use it in GitHub Desktop.
Quick node.js script to convert a CSV file to HTML
// first of all make sure we have enough arguments (exit if not)
if (process.argv.length != 5)
{
console.error("Usage: node csv2html.js input.csv template.ejs output.html")
console.error();
console.error("Outputs the given template for each row in the given input.")
console.error("Uses the first row of the CSV as column names in the template.")
process.exit(1);
}
// now load the modules we need
var csv = require('csv'), // library for processing CSV spreadsheet files
ejs = require('ejs'), // library for turning .ejs templates into .html files
fs = require('fs'), // node.js library for reading and writing files
assert = require('assert'); // node.js library for testing for error conditions
// make sure EJS is configured to use curly braces for templates
ejs.open = '{{';
ejs.close = '}}';
// grab the file names from the command arguments array
// and give them more convenient names
var inputFile = process.argv[2];
var templateFile = process.argv[3];
var outputFile = process.argv[4];
// make sure each file is the right type (exit if not)
assert.ok(inputFile.lastIndexOf('csv') == (inputFile.length - 'csv'.length), "input file should be a .csv file");
assert.ok(templateFile.lastIndexOf('ejs') == (templateFile.length - 'ejs'.length), "template file should be an .ejs file");
assert.ok(outputFile.lastIndexOf('html') == (outputFile.length - 'html'.length), "output file should be an .html file");
// make sure we use the correct line-endings on Windows
var EOL = (process.platform === 'win32' ? '\r\n' : '\n')
// build the template
var template = ejs.compile(fs.readFileSync(templateFile, 'utf8'))
// make an array to store our output
var outLines = [];
csv()
.fromPath(__dirname+'/'+inputFile, { columns: true })
.transform(function(data){
// optional transform step, e.g.
//data['Year'] = new Date(data['Date']).getUTCFullYear();
return data;
})
.on('data',function(data,index){
//console.log('#'+index+' '+JSON.stringify(data));
try {
outLines.push(template(data));
} catch (e) {
console.error(e.stack)
}
})
.on('end',function(count){
fs.writeFileSync(outputFile, outLines.join(EOL + EOL), 'utf8')
console.log("done!");
})
.on('error',function(error){
console.log(error.message);
});
Title Link Source Year Description
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
A Thing Title http://example.com Somewhere special 2012 A longer description of a thing
{
"author": "Tom Carden",
"name": "csv2html.js",
"description": "Convert a CSV file into an HTML file using a simple template.",
"version": "0.0.0",
"repository": {
"url": ""
},
"main": "csv2html.js",
"dependencies": {
"csv": "0.0.10",
"ejs": "0.6.1"
},
"devDependencies": {}
}
<h4><a title="{{=Title}}" href="{{=Link}}" target="_blank">{{=Title}}</a> <em>{{=Source}}</em> ({{=Year}})</h4>
{{=Description}}
@suriyaJaay
Copy link

suriyaJaay commented Dec 30, 2016

am unable to understand your above code about how to run this ? could you pls open repo/tutorial for how upload csv using MEAN stack and how to load csv data from db to html/view page(table)?

Thank you...

@billrafferty
Copy link

To run this you need to install EJS with NPM;

$ npm install ejs

Then to run the above and create a new file called output.html;

$ node csv2html.js input.csv template.ejs output.html

More info found here Embedded JavaScript templating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment