Some time ago due to project requirements, to do Excel and Word export function, Excel export Baidu A lot, the small partners are written in a good detail, the basic dozen to change can be used, can be exported word function, Baidu looks like can not find available information, ah, inexplicable ah. Later to find colleagues, colleagues have not the whole, it seems to have to go on it ...
The first time found that Baidu Google this powerful, colleagues recommended http://www.baigoogledu.com/
Baidu Google together Nodejs word find https://github.com/Ziv-Barber/officegen see here you always do not immediately understand, source introduction good detail, look carefully
NPM Install Officegen installs the Office plugin, which supports exporting Word, Excel, PPT
Below is attached my word export code, the code is a bit rough, you veteran to see ha
var Officegen = require (' Officegen ');
var fs = require (' FS ');
var path = require (' path ');
var docx = Officegen (' docx ');
var async = require (' async ');
/**
* Export Word
*/
Exports.exportword = function (req, res) {
Console.log (' Exportword-------------');
Docx.on (' Finalize ', function (written) {
Console.log (' Finish to create Word file.\ntotal bytes created: ' + written + ' \ n ');
});
Docx.on (' Error ', function (err) {
Console.log (ERR);
});
var pObj = Docx.createp ({align: ' center '});//create Row Set Center
Pobj.addtext ("hemodialysis (filtered) Treatment informed consent", {bold:true,font_face: ' Arial ', font_size:18});//Add text to set the font style bold size
var pObj = Docx.createp ();
Pobj.addtext (' name ');
Pobj.addtext (' with color ', {color: ' 000088 '});//Set Font color
Pobj.addtext (' sex ');
Pobj.addtext (', {color: ' 00ffff ', Back: ' 000088 '});
Pobj.addtext (' age ');
Pobj.addtext (' old ', {color: ' 000088 '});
var pObj = Docx.createp ();
Pobj.addtext (' outpatient (inpatient) number ');
Pobj.addtext (' with color ', {color: ' 000088 '});
Pobj.addtext (' diagnosis ');
Pobj.addtext (', {color: ' 000088 '});
var pObj = Docx.createp ();
Pobj.addtext (' One, hemodialysis (filtration) can effectively remove excess water and mildew in the body, is an effective treatment for acute and chronic renal failure and other diseases. ‘ );
var pObj = Docx.createp ();
Pobj.addtext (' II, hemodialysis (filtration) treatment, the first need to bring the patient's blood into the body, and then through dialysis or filtration and other methods to remove water and mildew, after receiving the blood back to the patient body. ‘ );
var pObj = Docx.createp ();
Pobj.addtext (' III) in order to effectively elicit blood, before treatment need to establish a vascular pathway (arteriovenous internal asthenia or deep venous intubation). ‘ );
var pObj = Docx.createp ();
Pobj.addtext (' IV) in order to prevent the coagulation of blood in vitro and dialysis, it is generally necessary to inject heparin and other anticoagulant drugs before and during dialysis. ‘ );
var pObj = Docx.createp ();
Pobj.addtext (' V ') The following medical risks are present during and during the hemodialysis process and may cause serious consequences and even endanger life: ');
var pObj = Docx.createp ();
Pobj.addtext (' 1. Hypotension, heart failure, myocardial infarction, arrhythmia, cerebrovascular accident; ');
var pObj = Docx.createp ();
Pobj.addtext (' 2. Air ball embolism; ');
var pObj = Docx.createp ();
Pobj.addtext (' 3. Allergic reactions; ');
var out = Fs.createwritestream (' out.docx ');//File Write
Out.on (' Error ', function (err) {
Console.log (ERR);
});
var result = Docx.generate (out);//server generates Word
Res.writehead (200, {
Note Here the type setting, export different file type value different application/vnd.openxmlformats-officedocument.presentationml.presentation
"Content-type": "Application/vnd.openxmlformats-officedocument.wordprocessingml.document",
' Content-disposition ': ' Attachment; Filename=out.docx '
});
Docx.generate (res);//Client Export Word
}
Nodejs Generating and exporting word