标签:运维 百度 运维管理 report ref follow 返回 typeof break
首先引入officegen:
var officegen = require(‘officegen‘);//之后是代码中使用了:
router.get(‘/exportWord‘, function (req, res) {
try {
var docx = officegen(‘docx‘);//这里这个在方法中使用,因为看了百度就一篇文章,放在开头
全局,这样导致导出的数据重复,这个苦难了很久,还是同事给力
var meet_name = req.query.meet_name;
//通过会议名称查询会议信息,议题信息
service.getMeetInfo(1, 100000, meet_name, function (meet) {
if (meet.rows[0].t_begin == "" || meet.rows[0].address == "") {
res.send("0");
} else {
/
判断文件是否存在
/
var filedir = ‘./public/uploadFile/temporaryFile/‘;
fs.ensureDir(filedir, function (err) {
});
fs.exists(filedir + meet_name + ‘_会议通知.docx‘, function (r) {
if (r) {
fs.unlinkSync(filedir + meet_name + ‘_会议通知.docx‘, function (err) {
if (err) throw err;
})
}
/
获取数据,处理数据
/
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 + ",";//参会科室
if (result.rows[i].involv_dept != "不涉及") {
str += result.rows[i].involv_dept + ",";//涉及科室
}
}
var strArray = str.split(",");
var report = [];
//去重
for (var k = 0; k < strArray.length; k++) {
if (strArray[k] != "质量管理室" && strArray[k] != "运维管理室" && strArray[k] != "稽核资管室") {
if (strArray.indexOf(strArray[k]) == k) report.push(strArray[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]) == undefined) {
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 st;
if (strs == "") {
st = ";";
} else {
st = "、";
}
/*
换行
*/
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 pObj4 = docx.createP({ align: ‘center‘ });// 创建行 设置居中
//pObj4.addText(meet.rows[0].meet_name + ‘会议通知‘, { bold: true, font_face: ‘Arial‘, font_size: 18 });
// 添加文字 设置字体样式 加粗 大小
//写入数据
var pObj = docx.createP();
pObj.addText(‘会议名称:‘ ,{bold: true});
pObj.addText(meet.rows[0].meet_name)
var pObj = docx.createP();
pObj.addText(‘会议时间:‘ ,{bold:true});// 设置字体颜色
pObj.addText( meet.rows[0].t_begin);
var pObj = docx.createP();
pObj.addText(‘会议地点:‘ ,{bold:true});
pObj.addText(meet.rows[0].address);
var pObj = docx.createP();
pObj.addText(‘参会科室:‘ ,{bold:true});
pObj.addText(‘质量管理室、运维管理室、稽核资管室‘+ st + strs );
var pObj = docx.createP();
pObj.addText(‘本次会议内容请见下方汇总表,目前已收集到议题【‘);
pObj.addText("" + result.total + "", { color: ‘FF0000‘ });
pObj.addText(‘】个。请议题汇报科室及涉及参会科室提前做好准备。‘);
var out = fs.createWriteStream(filedir + meet_name + ‘_会议通知.docx‘);// 文件写入
docx.generate(out);// 服务端生成word
out.on(‘error‘, function (err) {
console.log(err);
});
out.on(‘finish‘, function () {
console.log("写入完成。");
/*
设置5分钟删除生成的word,避免占用服务器资源
*/
setTimeout(() => {
try {
fs.access(‘./public/uploadFile/temporaryFile/‘ + meet_name + ‘_会议通知.docx‘,
fs.constants.F_OK, (err) => {
if (!err) {
fs.unlinkSync(‘./public/uploadFile/temporaryFile/‘ +
meet_name + ‘_会议通知.docx‘);
}
});
} catch (e) {
console.error(e);
}
}, 5 * 60 * 1000);
//返回服务器中生成的word的路劲
res.send(‘/uploadFile/temporaryFile/‘ + meet_name + ‘_会议通知.docx‘);
});
最后:页面通过路径直接下载word,这里附上页面ajax成功后的代码;
success: function (r) {
$.messager.progress(‘close‘);
if (r == "err") {
msgError(‘导出失败!,请稍后重试‘);
}
if (r == "0") {
msgError("请对会议基础信息进行完善和确认");
} else {
//获取当前网址,如: http://localhost:8080/imgs/doc/abc.jsp
var curWwwPath = window.document.location.href;
//获取主机地址之后的目录,如: imgs/doc/abc.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8080
var localhostPaht = curWwwPath.substring(0, pos);
location.href = localhostPaht + r;
}
},
标签:运维 百度 运维管理 report ref follow 返回 typeof break
原文地址:http://blog.51cto.com/13803916/2133602