【问题标题】:HTTP GET to generate Excel workbook, respond with Express, and then auto-download file?HTTP GET 生成 Excel 工作簿,用 Express 响应,然后自动下载文件?
【发布时间】:2017-08-23 22:13:02
【问题描述】:

我正在构建我的第一个自定义 NodeJS 服务器,我想要一些关于如何实施以下过程的指导和建议:

  1. 客户端使用一些查询参数向服务器发送 HTTP GET 请求
  2. 根据查询参数查询 MongoDB(使用 Mongoose)
  3. 从生成的 MongoDB 数据生成 Excel 工作簿
  4. 文件作为 HTTP 响应发送到客户端(使用 Express)
  5. 收到响应后,客户端浏览器会自动开始下载 Excel 工作簿文件。

我对第 3-5 部分特别迷茫。我不确定我应该使用什么工具在带有 Node 的服务器上生成 Excel 工作簿,哪种方式最好将 Excel 文件作为 HTTP 响应发送,或者如何让客户端浏览器开始下载接收到的文件。

谢谢!

【问题讨论】:

    标签: javascript node.js excel http express


    【解决方案1】:

    对于 3. 点使用任何这样的 npm 包

    JS代码

      /* Read Excel */
      mongoXlsx.xlsx2MongoData("./file.xlsx", model, function(err, mongoData) {
        console.log('Mongo data:', mongoData); 
      });
    

    参考链接mongo-xlsx

    对于第 5 点,请使用以下

        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);
        });
    

    您必须将此代码保留在您的 api 回调函数下

    【讨论】:

      【解决方案2】:

      请查看:https://www.npmjs.com/package/excel-export

      在这个模块介绍页面中,有一个很好的例子说明如何:

      • 使用excel-export生成excel文件内容。
      • 通过 Express 将 excel 文件内容发送到浏览器。
      • 让浏览器通过Content-Disposition自动下载excel文件

      这是页面上的示例代码:

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

      【讨论】:

      • 存储库已更新,此脚本不再工作
      猜你喜欢
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      相关资源
      最近更新 更多