The example in this article tells you how to export Excel Nodejs. Share to everyone for your reference. Specifically as follows:
Nodejs generates Excel for query data and downloads it using the way Mister Cost Excel file and then downloads it by comparing the Excel-export plug-in code as follows:
Excel.js Code:
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;}; /** * Generate Excel File * @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);}); /** * Computes last breakpoint information * @param range * @returns {number} * @private/excel.prototype._calstartposition = function (range) {V
Ar 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;
If the startpos is 0, it means that the file was downloaded from 0, otherwise the breakpoint was downloaded. 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, S Tate) {if (error) throw error; config.filesize = state.size; var range = Self.req.headers.range; config.startpos = Self._c
Alstartposition (range);
self.config = config;
Self._configheader (config);
Down ();
}); /** * Download File * @param filePath file path * @param req * @param res * @param isdeleted delete files after download is complete, true delete */Excel.prototype.dow nload = function (filepath,req,res,isdeleted) {var self = this; self.req=req; self.resp = res; path.exists (FilePath, funct Ion (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 () {//whether to delete file if (isdeleted) {fs.unlink (FilePath, function (err, res) {}), Resp.end ();}
);
}); } else {console.log (' file does not exist!
');
Return
}
});
} module.exports=new Excel ();
Call Mode:
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 = "Export 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);}}
);
I hope this article will help you with the NODEJS program design.