Note: You need to understand the template to accept the data format, not directly to get up on it, usually not to have a match degree.
- Extracting data from the database
The roughly processed data format is:
These are all ready you need something, template;
<%forrbegin rs,i in data[0]%>: start loop; Because the data is encapsulated as
[
[{},{},{},{}.....]
]; so through data[0],
You can take out all the data and apply the array in the array;
<%forrend%>: End loop;
<%=RS.ISSUE_NAME%>: Use your encapsulated key to fetch values;
Now is the Code section
- Join Ejsexcel
var ejsexcel = require (' Ejsexcel ');
var fs = require (' FS ');
Of course, you need to add this module before using (NPM install ejsexcel)
./GET Export Excel Test. Exporting Excel Topics/
Router.get ('/exportexcel ', function (req, res, next) {
var filedir = './public/uploadfile/temporaryfile/';
Fs.ensuredir (Filedir, function (err) {
});
var meet_name = Req.query.meet_name;
try {
Before exporting, determine if the file to be exported exists, according to the requirements
Fs.exists ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', function (r) {
if (r) {
Fs.unlink ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', function (err) {
if (err) throw err;
})
}
Issueservice.getissueinfo (1, 10000, meet_name, function (result) {
Querying data
if (Result.rows.length < 1) {
No data returned 0 page get the return value to make a corresponding prompt
Res.send ("0");
} else {
Fs.ensuredir (Filedir, function (err) {});
(Async function () {
var datas = [];
var data = [];
for (var i = 0; i < result.rows.length; i++) {
/*
Processing data for the above format
/
Data.push (Result.rows[i]);
}
Datas.push (data);
To get the buffer object for an Excel template
Const EXLBUF = await Readfileasync ('./public/uploadfile/template/issue.xlsx ');
Render an Excel template with data source (object)
Const EXLBUF2 = await ejsexcel.renderexcel (exlbuf, datas);
Await Writefileasync ('./public/uploadfile/temporaryfile/' + Meet_name + ' _ Issue report. xlsx ', exlBuf2);
SetTimeout (() = {
Console.info ("delete issue");
try {
Fs.access ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ', Fs.constants.F_OK,
(err) = {
if (!err) {
Fs.unlinksync ('./public/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ')
}
});
} catch (e) {
Console.info ("exception");
Console.error (e);
}
}, 5 -1000);
Returns the generated table path for download
Res.send ('/uploadfile/temporaryfile/' + meet_name + ' _xxx.xlsx ');
})();
}
});
});
} catch (Error) {
Return exception
Res.send ("err");
}
});
Note: At any time, the code problems do not catch the urgent, see if the error is not a module, or quotation marks, and so on, in short
The more edgy the new middle, the more time it takes to solve the problem
Nodejs+express using Ejsexcel to do complex export excel