1

我正在使用 exceljs 生成一个 excel 文件(使用它的流功能)。我不想将文件保存在服务器上然后读取并返回它,而是直接将 exceljs 流流式传输到 hapijs 中的响应。

function(request, reply) {
  const options = {
    stream: reply, // I need to somehow declare the reply stream here
    useStyles: false,
    useSharedStrings: true
  };

  const workbook = new Excel.stream.xlsx.WorkbookWriter(options);
  workbook.addWorksheet('Test');
  const worksheet = workbook.getWorksheet('Test');

  worksheet.columns = [{
    header: 'product', key: 'product'
  }];

  worksheet.addRow({product: 'MyProduct'}).commit();
  worksheet.commit();
  workbook.commit();

这可能吗?我不能像在 expressjs 中那样直接将回复传递给 Exceljs 流的选项?有什么想法吗?

4

1 回答 1

1
var wb = new Excel.stream.xlsx.WorkbookWriter();
var sh = wb.addWorksheet('sheet1');
sh.columns = [
    { header: 'Name', key: 'name' },
    { header: 'Age', key: 'age' }
];

sh.addRow(['Ben', 8]);
sh.addRow(['Dave', 10]);

sh.commit();
wb.commit()
.then(function () {
    var stream = wb.stream;
    var b = stream.read();

    reply(b)
    .type('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    .header('Content-Disposition', 'attachment; filename=file.xlsx')
    .header('Content-Length', stream.length);
});
于 2017-04-25T06:47:57.800 回答