Use nodejs to export an excel file. Use nodejs to export an excel file.
This document describes how to export an excel file from nodejs. Share it with you for your reference. The details are as follows:
Nodejs generates and downloads excel files for query data. The excel-export plug-in code is 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 an 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 | ""; va R result = excelExport.exe cute (data); var name1_'excel'{uid}'.xlsx'; var filePath = path. resolve (setting. savePath, name); fs. writeFile (filePath, result, 'binary ', function (err) {setting. cb (filePath) ;});}/*** calculate the last breakpoint information * @ 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; // if the value of startPos is 0, the object is downloaded starting from 0. Otherwise, the object is downloaded from a breakpoint. 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. f IleSize = state. size; var range = self. req. headers. range; config. startPos = self. _ calStartPosition (range); self. config = config; self. _ configHeader (config); down ();});} /*** download file ** @ param filePath file path * @ param req * @ param res * @ param isDeleted whether to delete the file after the download is complete. true: delete the file */excel. prototype. download = function (filePath, req, res, isDeleted) {var self = this; self. req = req; self. resp = res; path. exists (filePath, function (ex Ist) {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 () {// whether to delete the object if (isDeleted) {fs. unlink (filePath, function (err, res ){}) ;}Resp. end () ;}) ;}} else {console. log ('the file does not exist! '); Return ;}}) ;}module. exports = new excel ();
Call method:
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 ', '2017-06-29', true, 2015], ["e", '2017-06-29 ', false, 3.14]; 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 design nodejs programs.