标签:
前段时间由于项目需求,得做excel和word的导出功能,excel的导出百度一下一大把,小伙伴们都写的好详细,基本打来改改就可以用,可导出word的功能,百度了貌似都找不到可用资料,哎,费解呀。后来找同事,同事们也没整过,看来还得自己上呀。。。
第一次发现原来百度Google这强大,同事推荐的 http://www.baigoogledu.com/
百度Google一起摆 nodejs word 找到https://github.com/Ziv-Barber/officegen 看到这里您老是不就瞬间明白了,源码介绍好详细,认真看
npm install officegen 安装office插件,支持导出word、excel、ppt
下边附上我的word导出代码,代码有点糙,您老将就看哈
var officegen = require(‘officegen‘);
var fs = require(‘fs‘);
var path = require(‘path‘);
var docx = officegen ( ‘docx‘ );
var async = require(‘async‘);
/**
* 导出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‘ } );// 创建行 设置居中
pObj.addText ( ‘血液透析(滤过)治疗知情同意书‘, { bold: true,font_face: ‘Arial‘, font_size: 18 });// 添加文字 设置字体样式 加粗 大小
var pObj = docx.createP ();
pObj.addText ( ‘姓名‘ );
pObj.addText ( ‘ with color‘, { color: ‘000088‘ } );// 设置字体颜色
pObj.addText ( ‘性别‘ );
pObj.addText ( ‘‘, { color: ‘00ffff‘, back: ‘000088‘ } );
pObj.addText ( ‘年龄‘ );
pObj.addText ( ‘岁‘, { color: ‘000088‘ } );
var pObj = docx.createP ();
pObj.addText ( ‘门诊(住院)号‘ );
pObj.addText ( ‘ with color‘, { color: ‘000088‘ } );
pObj.addText ( ‘诊断‘ );
pObj.addText ( ‘‘, { color: ‘000088‘} );
var pObj = docx.createP ();
pObj.addText ( ‘一、血液透析(滤过)能有效清除身体内过多的水分合霉素,是治疗急性和慢性肾衰竭等疾病的有效方法。‘ );
var pObj = docx.createP ();
pObj.addText ( ‘二、血液透析(滤过)治疗时,首先需要将患者血液引到体外,然后通过透析或滤过等方法清除水分和霉素,经受理后的血液再回到患者体外。‘ );
var pObj = docx.createP ();
pObj.addText ( ‘三、为了有效引出血液,治疗前需要建立血管通路(动静脉内痿或深静脉插管)。‘ );
var pObj = docx.createP ();
pObj.addText ( ‘四、为防止血液在体外管路和透析器发生凝固,一般需要在透析前和透析过程中注射肝素等抗凝药物。‘ );
var pObj = docx.createP ();
pObj.addText ( ‘五、血透过程中和治疗期间存在下列医疗风险,可能造成严重后果,甚至危及生命:‘ );
var pObj = docx.createP ();
pObj.addText ( ‘1.低血压,心力衰竭,心肌梗塞,心律失常,脑血管意外;‘ );
var pObj = docx.createP ();
pObj.addText ( ‘2.空气球栓塞;‘ );
var pObj = docx.createP ();
pObj.addText ( ‘3.过敏反应;‘ );
var out = fs.createWriteStream ( ‘out.docx‘ );// 文件写入
out.on ( ‘error‘, function ( err ) {
console.log ( err );
});
var result = docx.generate (out);// 服务端生成word
res.writeHead ( 200, {
// 注意这里的type设置,导出不同文件type值不同application/vnd.openxmlformats-officedocument.presentationml.presentation
"Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
‘Content-disposition‘: ‘attachment; filename=out.docx‘
});
docx.generate (res);// 客户端导出word
}
标签:
原文地址:http://blog.csdn.net/liyanhui1001/article/details/45242359