Search code examples
javascriptnode.jsexcelbrowserexcel4node

Excel spreadsheet not downloading


I have created an excel spreadsheet using the Excel4node library and then filled out this spreadsheet with the necessary data.

var excel = require('excel4node');
var workbook = new excel.Workbook();
var worksheet = workbook.addWorksheet('Sheet 1');

I then want the user to be able to download this spreadsheet and open it in Microsoft Excel.

workbook.write("Excel.xlsx", res);

This code works in the FireFox browser, however no download is prompted in Google Chrome or Microsoft Edge. Can anyone help me fix this issue? Any help would be much appreciated!


Solution

  • Forgot to set content-type header otherwise browser has to guess.

    wb.writeToBuffer()
      .then(function(buffer) { 
       const filename = 'Testing workbook.xlsx'
        res
          .set('content-disposition', `attachment; filename="${filename}";  filename*=UTF-8''${encodeURI(filename)}`) // filename header
          .type('.xlsx') // setting content-type to xlsx. based on file extention
          .send(buffer) 
      });