0

Suppose I want to use exceljs to serve excel to web clients. Also, suppose that I am using streaming (due to the files being potentially large and not wanted to keep them in memory).

Is it possible to find out what the Content-Length header should be? Because without this, I can't see the download progress spinner from chrome.

Example code:

app.get('/some/route', function(req, res) {
  res.writeHead(200, {
    'Content-Disposition': 'attachment; filename="file.xlsx"',
    'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    "Content-Length" : inf //this is what I want!!
  })
  var workbook = new Excel.stream.xlsx.WorkbookWriter({ stream: res })
  var worksheet = workbook.addWorksheet('some-worksheet')
  worksheet.addRow(['foo', 'bar']).commit()
  worksheet.commit()
  workbook.commit()
}
nodel
  • 471
  • 6
  • 22

2 Answers2

0

try this.

res.set({
  'Content-Disposition': 'attachment; filename="ireshansexcel.xlsx"',
  'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});

 const workbook = new Excel.Workbook();
 const worksheet = workbook.addWorksheet('Survey');
 //your excel worksheet content
 //commit everything
 let fileBuffer = await workbook.xlsx.writeBuffer();
 res.send(fileBuffer);

I have been using writeBuffer() method in exceljs and seems it's working fine with progress spinner. But I have worked with small files. But you can test this approach. I used this approach because above you mentioned method didn't worked for me in azure functions.

0

in addition to @ireshan pathirana answer, workbook.xlsx.write can be used, no file buffer needed (

res.set({
  'Content-Disposition': 'attachment; filename="ireshansexcel.xlsx"',
  'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});

 const workbook = new Excel.Workbook();
 const worksheet = workbook.addWorksheet('Survey');
 //your excel worksheet content
 //commit everything
 workbook.xlsx.write(res);

Sunny
  • 1
  • 1