1

我有一个exceljs用来生成 excel 文件的sails 应用程序。查看他们的文档:

https://www.npmjs.com/package/exceljs#writing-xlsx

看来他们允许写入流。

// write to a stream 
workbook.xlsx.write(stream)
    .then(function() {
        // done 
    });

当用户期待响应时,我该怎么做?

我阅读了以下内容:

http://sailsjs.org/documentation/concepts/custom-responses/adding-a-custom-response

http://sailsjs.org/documentation/concepts/custom-responses/default-responses

res.attachment()保存文件后我也尝试过,但没有成功。有什么我想念的吗?

编辑

下面的答案几乎是正确的。我做了以下,它的工作。以前的解决方案有时会损坏文件。

    res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
    return workbook.xlsx.write(res)
      .then(function() {
        res.end();
      });

这适用于我的 EC2 实例和本地。

4

1 回答 1

2

解决方案:https ://github.com/guyonroche/exceljs/issues/37将工作簿写入响应。

var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var sheet = workbook.addWorksheet("My Sheet");
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
workbook.xlsx.write(res);
res.end();
于 2016-09-05T21:15:49.237 回答