admin管理员组文章数量:1426006
I'm building my first custom NodeJS server and I'd like some guidance and advice on how to implement the following procedure:
- Client sends an HTTP GET request to server with some query parameters
- Query MongoDB based on query parameters (using Mongoose)
- An Excel workbook is generated from the resulting MongoDB data
- The file is sent to the client as an HTTP response (using Express)
- Upon receiving the response, the client browser automatically starts downloading the Excel workbook file.
I'm particularly lost on parts 3-5. I'm not sure what tools I should use to generate an Excel workbook on the server with Node, which way is best to send an Excel file as an HTTP response, or how to make the client browser start downloading the received file.
Thanks!
I'm building my first custom NodeJS server and I'd like some guidance and advice on how to implement the following procedure:
- Client sends an HTTP GET request to server with some query parameters
- Query MongoDB based on query parameters (using Mongoose)
- An Excel workbook is generated from the resulting MongoDB data
- The file is sent to the client as an HTTP response (using Express)
- Upon receiving the response, the client browser automatically starts downloading the Excel workbook file.
I'm particularly lost on parts 3-5. I'm not sure what tools I should use to generate an Excel workbook on the server with Node, which way is best to send an Excel file as an HTTP response, or how to make the client browser start downloading the received file.
Thanks!
Share Improve this question asked Mar 30, 2017 at 6:31 OkonomiyakiOkonomiyaki 431 gold badge1 silver badge4 bronze badges2 Answers
Reset to default 1For 3. point use any package of npm like this
Js code
/* Read Excel */
mongoXlsx.xlsx2MongoData("./file.xlsx", model, function(err, mongoData) {
console.log('Mongo data:', mongoData);
});
refer linkmongo-xlsx
For point 5 use following
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("file.jpg");
var request = http.get("./file.xlsx", function(response) {
response.pipe(file);
});
You have to keep this code under your api callback function
Please check: https://www.npmjs./package/excel-export
In this module introduction page, there is a very good example on how to:
- Generate excel file content with
excel-export
. - Send excel file content to browser via Express.
- Make browser download the excel file automatically via
Content-Disposition
Here is the example code on the page:
var express = require('express');
var nodeExcel = require('excel-export');
var app = express();
app.get('/Excel', function(req, res){
var conf ={};
conf.stylesXmlFile = "styles.xml";
conf.name = "mysheet";
conf.cols = [{
caption:'string',
type:'string',
beforeCellWrite:function(row, cellData){
return cellData.toUpperCase();
},
width:28.7109375
},{
caption:'date',
type:'date',
beforeCellWrite:function(){
var originDate = new Date(Date.UTC(1899,11,30));
return function(row, cellData, eOpt){
if (eOpt.rowNum%2){
eOpt.styleIndex = 1;
}
else{
eOpt.styleIndex = 2;
}
if (cellData === null){
eOpt.cellType = 'string';
return 'N/A';
} else
return (cellData - originDate) / (24 * 60 * 60 * 1000);
}
}()
},{
caption:'bool',
type:'bool'
},{
caption:'number',
type:'number'
}];
conf.rows = [
['pi', new Date(Date.UTC(2013, 4, 1)), true, 3.14],
["e", new Date(2012, 4, 1), false, 2.7182],
["M&M<>'", new Date(Date.UTC(2013, 6, 9)), false, 1.61803],
["null date", null, true, 1.414]
];
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
res.end(result, 'binary');
});
app.listen(3000);
console.log('Listening on port 3000');
本文标签:
版权声明:本文标题:javascript - HTTP GET to generate Excel workbook, respond with Express, and then auto-download file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745444831a2658614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论