Previous projects have been using the Nodejs node-xlsx.js middleware, the most recent project deployment can not use the external network, and node-xlsx.js path is too long cannot be copied, now use Xlsx.js middleware.
Now to share your own encapsulation of the use of the way to learn with everyone.
The code is simple and lightly sprayed.
/** * Created by Administrator on 2016/11/17. * Using XLSX Middleware, incoming header [array], in-table data [object array], file name [string]//var header=["name", ' age ', ' gender ']; * var excel=[{name: ' Z3 ', age:16,sex: ' Man '}, {name: ' L4 ', Age:20,sex: ' Female '}, {name: ' W5 ', age:24,sex: ' Man '}, {name: ' Z6 ', age:23, Sex: ' Female '}, {name: ' ss ', Age:10,sex: ' No male no female '}]; var path= '. /web/xxx.xlsx '; */var fs=require (' FS '), var os=require (' OS '), var xlsx=require (' xlsx '), var errors=require ('.. /libs/errors '), var underscore = require (' underscore '), function Createexcel (headarr,excel,filepath,callback) {if (! Headarr instanceof Array | | !excel instanceof Array | | typeof FilePath! = ' string ') {//console.log (Headarr instanceof Array, Excel instanceof array,typeof filePath); Return callback ("parameter pass error required to create file method", null); }else{var arrlen=headarr.length; var headobj={},//generate table header indexobj={};//generate table data for (Var i=0;i<arrlen;i++) {var STR=STRING.FROMC Harcode (65+i) +1; if (i>26) {Str=string.fromcharcode (+) +string.fromCharCode (65+i-26) +1; } if (i>52) {return callback (' table header too much, please do appropriate deletions ', null); } Headobj[str]={v:headarr[i]}; } var indexarr=[]; for (Var e=0;e<excel.length;e++) {for (Var j=0;j
NODEJS middleware Xlsx.js Export Excel file using method Encapsulation (original)