萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> nodejs導出excel的方法

nodejs導出excel的方法

   本文實例講述了nodejs導出excel的方法。分享給大家供大家參考。具體如下:

  nodejs 對查詢數據生成excel並下載,采用方式先生成本excel文件,然後再下載;通過比較采用excel-export插件代碼如下:

  excel.js代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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.config resp = 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();

  調用方式:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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程序設計有所幫助。

copyright © 萬盛學電腦網 all rights reserved