node.js - 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.
解决方案
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");
});
推荐阅读
- c++ - 使用每个元素的重复条目创建一个向量
- javascript - 从 for 循环中排除给定值 - ES5
- laravel - 如何覆盖此文本 - 密码必须至少为 8 个字符
- android - 即使在使用 Firebase 进行身份验证后,Firebase 也不会发送 OTP
- php - 尝试在从数据库获取数据时使用 php 显示图像
- javascript - react native:设置标签导航 5 样式的方法是什么?
- java - 我的颤振应用程序没有从 release.apk 中的 api 获取数据,但在调试模式下工作
- angular - 为什么我在使用带有 waitForAngularEnabled(true) 的量角器运行 e2e 测试时出现错误
- python - matplotlib 动画适用于 Windows 和 Linux,但不适用于 Mac OS
- erlang - Rebar3:如何从 erlang 引用库的源工件?