如何以快递方式发送缓冲区数据?

时间:2018-06-27 12:49:50

标签: node.js express buffer

我正在尝试创建一个Excel文件并将其发送给客户端。客户端应该通过ajax请求下载文件,因为我需要过滤器参数。

我正在使用excel4node软件包创建一个excel文件。

我写下面的代码,现在可以使用,但是我怀疑如果我的数据大于缓冲区该怎么办。这是使用缓冲区的正确方法吗? (请使用writeToBuffer方法检查该行)

    const xl = require('excel4node');

    const excelCreator = function (data) {...}

    app.post('/api/excel', jsonParser, (req, res) => {

      let reqObj = {
        method: 'post',
        url: apiUrl + '/MemberService';,
        headers: {
          'Content-Type': 'application/json'
        },
        data: req.body
      };

      axios(reqObj)
        .then(response => {
          res.body = responseHandler(response); // a helper function to set res object

          let data = res.body.Data;

          res.setHeader('Content-Disposition', 'attachment; filename=' + 'excel.xlsx');
          res.type('application/octet-stream');
          res.body.Data = null;

          excelCreator(data).writeToBuffer().then(function (buffer) {
            res.body.Data = buffer;
            res.send(res.body);
          });

        })
        .catch(...);
    });

2 个答案:

答案 0 :(得分:2)

首先安装SheetJS js-xlsx

npm --save install xlsx

然后您可以使用Express发送此响应:

const xlsx = require('xlsx')

var wb = xlsx.utils.book_new();

var table = [['a', 'b', 'c'], ['1', '2', '3']]
var ws = xlsx.utils.aoa_to_sheet(table);
xlsx.utils.book_append_sheet(wb, ws, 'test');

// write options
const wopts = { bookType: 'xlsx', bookSST: false, type: 'base64' };
const buffer = xlsx.write(wb, wopts);

res.writeHead(200, [['Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']]);
res.end(new Buffer(buffer, 'base64'));

随时检查文档 https://www.npmjs.com/package/xlsx

答案 1 :(得分:1)

           Package used:
           npm install xlsx --save

            import * as XLSX from 'xlsx';

            var json =[
                {
                'Fullname' : "Ram Shrestha",
                'Date Of Birth':"1996-06-01",
                'Gender': "Male",
                'Salary':"80000",
                'Designation':"Senior"
               },
               {
                'Fullname' : "Ramesh Shrestha",
                'Date Of Birth':"1997-06-01",
                'Gender': "Male",
                'Salary':"90000",
                'Designation':"Senior"
              }
           ];

            const fileName = "Sample";

            const workBook: XLSX.WorkBook = XLSX.utils.book_new();
            
            const workSheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
    
            XLSX.utils.book_append_sheet(workBook, workSheet, `${excelFileName}`)
            const buffer = XLSX.write(workBook, { bookType: 'xlsx', bookSST: false, type: 'base64' });
    
            res.writeHead(200, [
                ['Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
                ["Content-Disposition", "attachment; filename=" + `${fileName}.xlsx`]
            ]);
            res.end(Buffer.from(buffer, 'base64'));

希望它能起作用。