首页 > 解决方案 > Nodejs - Export Excel not downloading excelfile

问题描述

  var nodeExcel = require('excel-export');
  var conf = {}
  conf.cols = [{
  caption: 'Sl.',
  type: 'number',
  width: 3
    }
  ];
  var arr = [];
  arr.push([1]);
  conf.rows = arr;
  var result = nodeExcel.execute(conf);
  res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  res.setHeader("Content-Disposition", "attachment; filename=" + 
   "Report.xlsx");
  res.writeHead(200);
  res.end(result, 'binary');

I am using excel-export node package to download excel file in browser.

标签: node.jsnpmnodes

解决方案


Works for me. Below code downloads the excel file in browser.

var express = require('express');
var app = express();
var nodeExcel = require('excel-export');
var conf = {}
conf.cols = [{
        caption: 'Sl.',
        type: 'number',
        width: 3
    }];
var arr = [];
arr.push([1]);
conf.rows = arr;

var result = nodeExcel.execute(conf);

app.get('/',function(req,res){
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=" + 
    "Report.xlsx");
    res.writeHead(200);
    res.end(result, 'binary');
});
app.listen(3000,function(){
    console.log("listening port 3000");
});

推荐阅读