First introduce Officegen:
var Officegen = require (' Officegen ');//after it is used in the code:
Router.get ('/exportword ', function (req, res) {
try {
var docx = Officegen (' docx ');//Here This is used in the method, because read Baidu on an article, put in the beginning
Global, this causes the exported data to repeat, this suffering for a long time, or a colleague to force
var meet_name = Req.query.meet_name;
Query meeting information through meeting name, topic information
Service.getmeetinfo (1, 100000, meet_name, function (meet) {
if (Meet.rows[0].t_begin = = "" | | Meet.rows[0].address = = "") {
Res.send ("0");
} else {
/
Determine if a file exists
/
var filedir = './public/uploadfile/temporaryfile/';
Fs.ensuredir (Filedir, function (err) {
});
Fs.exists (Filedir + meet_name + ' _ meeting notice. docx ', function (r) {
if (r) {
Fs.unlinksync (Filedir + meet_name + ' _ meeting notice. docx ', function (err) {
if (err) throw err;
})
}
/
Get data, process data
/
Issueservice.getissueinfo (1, 100000, meet_name, function (result) {
var str = ';
for (var i = 0; i < result.rows.length; i++) {
str + = result.rows[i].report_dept + ",";//Attendance sections
if (result.rows[i].involv_dept! = "does not involve") {
str + = result.rows[i].involv_dept + ",";//related departments
}
}
var strarray = Str.split (","); var report = []; Go to Heavy for (var k = 0; k < strarray.length; k++) {if (strarray[k]! = "Quality Management room" && strarray[k]! = "Operations Management Room" && strarray[k]! = "Audit Funding chamber") {if (Strarray.indexof (strarray[k]) = = k) Report.push (Strar Ray[k]); }} var ss = Report.tostring (). Split (","); var strs = '; for (var i = 0; i < ss.length; i++) {if (ss[i] = = "| | ss[i] = = NULL | | typeof (Ss[i]) = = Undefine d) {Ss.splice (I, 1); i = i-1; }} for (var i = 0; i < ss.length; i++) {if (i = = ss.length-1) { STRs + = Ss[i].split (","). ToString () + ";"; Break } STRs + = Ss[i] + ","; } var Qty if (STRs = = "") {st = ";"; } else {st = ","; }/* Line break */var POBJ = Docx.createp (); Pobj.addtext ("); var pObj = Docx.createp (); Pobj.addtext ("); var pObj = Docx.createp (); Pobj.addtext ("); var pObj = Docx.createp (); Pobj.addtext ("); /* Provide line break *///var PObj4 = Docx.createp ({align: ' center '});//Create Line Set Center//pobj4.addtext (meet.rows[0].meet_name + ' meeting notifications ', {bold:true, font_face: ' Arial ', font_size:18}); Add text set font style bold size//write data var pObj = Docx.createp (); Pobj.addtext (' Conference name: ', {bold:true}); Pobj.addtext (meet.rows[0].meet_name) var pObj = Docx.createp (); Pobj.addtext (' Meeting time: ', {bold:true});//Set Font color Pobj.addtext (meet.rows[0].t_begin); var pObj = Docx.createp (); Pobj.addtext (' Meeting place: ', {bold:true}); Pobj.addtext (meet.rows[0].address); var pObj = Docx.createp (); Pobj.addtext (' Participating departments: ', {bold:true}); Pobj.addtext (' Quality control room, operation and maintenance management room, Audit capital control room ' + st + STRs); var pObj = Docx.createp (); Pobj.addtext (' The contents of this meeting, please see the summary table below, has now collected the topic "'); Pobj.addtext ("" + Result.total + "", {color: ' FF0000 '}); Pobj.addtext (' "A. Please prepare the issue report department and the participating departments in advance. ‘); var out = Fs.createwritestream (Filedir + meet_name + ' _ meeting notification. docx ');//File Write Docx.generate (out);//Server Generation W Ord Out.on (' Error ', function (err) { Console.log (ERR); }); Out.on (' Finish ', function () {Console.log ("write complete. "); /* Set 5 minutes to delete the generated word, avoiding server resources */SetTimeout (() = {try {fs.access ('./public/upload file/temporaryfile/' + Meet_name + ' _ meeting notice. docx ', Fs.constants.F_OK, (err) = {if (!err) { Fs.unlinksync ('./public/uploadfile/temporaryfile/' + meet_n Ame + ' _ meeting notice. docx '); } }); } catch (e) {consolE.error (e); }}, 5 * 60 * 1000); Returns the Res.send ('/uploadfile/temporaryfile/' + Meet_name + ' _ meeting notification. docx ') generated by word in the server. });
Finally: the page directly through the path to download word, here is attached to the page Ajax after the successful code;
Success:function (R) {
$.messager.progress (' close ');
if (r = = "Err") {
Msgerror (' Export failed!, please retry later ');
}
if (r = = "0") {
Msgerror ("Please complete and confirm the basic information of the Meeting");
} else {
Get the current URL, such as: http://localhost:8080/imgs/doc/abc.jsp
var curwwwpath = Window.document.location.href;
Gets the directory after the host address, such as: imgs/doc/abc.jsp
var pathName = Window.document.location.pathname;
var pos = curwwwpath.indexof (pathName);
Get host address, such as: http://localhost:8080
var localhostpaht = curwwwpath.substring (0, POS);
Location.href = Localhostpaht + R;
}
},