Skip to content

Commit

Permalink
excel report can be generated now
Browse files Browse the repository at this point in the history
  • Loading branch information
tareq89 committed Oct 2, 2016
1 parent 2665304 commit bafef7e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Binary file added excel/1475400462596sample.xlsx
Binary file not shown.
Binary file added excel/1475400482003sample.xlsx
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cors": "^2.8.1",
"express": "^4.14.0",
"mongodb": "^2.2.10",
"mongoose": "^4.6.0"
"mongoose": "^4.6.0",
"msexcel-builder": "0.0.2"
}
}
29 changes: 27 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var express = require('express');
var excelbuilder = require('msexcel-builder');
var calculate = require('./calculate');
var utility = require('./utility');
var queryMaker = require('./queryMaker');
Expand Down Expand Up @@ -38,15 +39,39 @@ router.get('/report', function (req, res) {
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
var query = queryMaker.reportQuery(req);
console.log(req.query.generateexcel);
console.log(query)
var cursor = db.collection('Jobs').find(query);
cursor.each(function (err, job) {
assert.equal(err, null);
if(job!=null){
report = calculate.summaryReport(req, report, job);
} else {
db.close();
res.json({ data: report });
db.close();
if (req.query.generateexcel == "true") {
var directoryName = __dirname + "\\excel";
var workbookName = Date.now().toString() + 'sample.xlsx';
var reportFileName = directoryName + "\\" + workbookName;

var workbook = excelbuilder.createWorkbook(directoryName, workbookName);
var sheet1 = workbook.createSheet('sheet1', 10, 12);
sheet1.set(1, 1, 'I am title');
for (var i = 2; i < 5; i++)
sheet1.set(i, 1, 'test'+i);

// Save it
workbook.save(function(ok){
if (!ok) {
// workbook.cancel();
console.log(ok)
console.log(reportFileName)
res.download(reportFileName);
} else
res.json({ date: "congratulations, your workbook created"});
});
} else {
res.json({ data: report });
}
}
})
});
Expand Down

0 comments on commit bafef7e

Please sign in to comment.