您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 商丘分类信息网,免费分类信息发布

nodejs导出excel的方法_node.js

2024/3/5 6:11:33发布32次查看
本文实例讲述了nodejs导出excel的方法。分享给大家供大家参考。具体如下:
nodejs 对查询数据生成excel并下载,采用方式先生成本excel文件,然后再下载;通过比较采用excel-export插件代码如下:
excel.js代码:
var extend = require(extend);var fs = require(fs);var excelexport = require('excel-export');var guid=require('guid');var path=require('path');var excel=function(){this.req=null;this.resp=null;};/*** 生成excel文件* @param params*/excel.prototype.createexcel=function(params){var setting={savepath:uploadfile/excel/};setting=extend({},setting,params);var uuid=guid.create();var data=params.data||;var result = excelexport.execute(data);var name='excel'+uuid+'.xlsx';var filepath= path.resolve(setting.savepath, name);fs.writefile(filepath, result, 'binary',function(err){setting.cb(filepath);});}/*** 计算上次的断点信息* @param range* @returns {number}* @private*/excel.prototype._calstartposition = function(range) {var startpos = 0;if( typeof range != 'undefined') {var startposmatch = /^bytes=([0-9]+)-$/.exec(range);startpos = number(startposmatch[1]);}return startpos;}excel.prototype._configheader = function(config) {var startpos = config.startpos,filesize = config.filesize,resp = this.resp;// 如果startpos为0,表示文件从0开始下载的,否则则表示是断点下载的。if(startpos == 0) {resp.setheader('accept-range', 'bytes');} else {resp.setheader('content-range', 'bytes ' + startpos + '-' + (filesize - 1) + '/' + filesize);}resp.writehead(206, 'partial content', {'content-type' : 'application/octet-stream'});}excel.prototype._init = function(filepath, down) {var config = {};var self = this;fs.stat(filepath, function(error, state) {if(error)throw error;config.filesize = state.size;var range = self.req.headers.range;config.startpos = self._calstartposition(range);self.config = config;self._configheader(config);down();});}/*** 下载文件* @param filepath 文件路径* @param req* @param res* @param isdeleted 下载完成后是否删除文件,true删除*/excel.prototype.download = function(filepath,req,res,isdeleted) {var self = this;self.req=req;self.resp = res;path.exists(filepath, function(exist) {if(exist) {self._init(filepath, function() {var config = self.configresp = self.resp;freadstream = fs.createreadstream(filepath, {encoding : 'binary',buffersize : 1024 * 1024,start : config.startpos,end : config.filesize});freadstream.on('data', function(chunk) {resp.write(chunk, 'binary');});freadstream.on('end', function() {//是否删除文件if(isdeleted) {fs.unlink(filepath, function (err, res) {});}resp.end();});});}else {console.log('文件不存在!');return;}});}module.exports=new excel();
调用方式:
var excel=require('../lib/excelhelper.js');exports.exportexcel=function(req,res){var conf ={};conf.cols = [{caption:'string', type:'string'},{caption:'date', type:'string'},{caption:'bool', type:'bool'},{caption:'number', type:'number'}];conf.rows = [['pi', '2015-06-29', true, 3.14],[e, '2015-06-29', false, 2.7182]];var filename =导出excel.xlsx;res.setheader('content-disposition', 'attachment; filename='+encodeuricomponent(filename));excel.createexcel({data:conf,savepath:uploadfile/excel/,cb:function(path){excel.download(path,req, res,true);}});}
希望本文所述对大家的nodejs程序设计有所帮助。
商丘分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录