NodeJS 如何在快递中发送缓冲区数据?

dl5txlt9  于 2023-01-12  发布在  Node.js
关注(0)|答案(2)|浏览(108)

我正在尝试创建一个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(...);
    });
hof1towb

hof1towb1#

首次安装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

oalqel3c

oalqel3c2#

所用 Package :

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

希望能成功。

相关问题