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:

  1. Client sends an HTTP GET request to server with some query parameters
  2. Query MongoDB based on query parameters (using Mongoose)
  3. An Excel workbook is generated from the resulting MongoDB data
  4. The file is sent to the client as an HTTP response (using Express)
  5. 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:

  1. Client sends an HTTP GET request to server with some query parameters
  2. Query MongoDB based on query parameters (using Mongoose)
  3. An Excel workbook is generated from the resulting MongoDB data
  4. The file is sent to the client as an HTTP response (using Express)
  5. 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 badges
Add a ment  | 

2 Answers 2

Reset to default 1

For 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');

本文标签: