您好,登錄后才能下訂單哦!
首先引入officegen:
var officegen = require('officegen');//之后是代碼中使用了:
router.get('/exportWord', function (req, res) {
try {
var docx = officegen('docx');//這里這個(gè)在方法中使用,因?yàn)榭戳税俣染鸵黄恼?,放在開(kāi)頭
全局,這樣導(dǎo)致導(dǎo)出的數(shù)據(jù)重復(fù),這個(gè)苦難了很久,還是同事給力
var meet_name = req.query.meet_name;
//通過(guò)會(huì)議名稱查詢會(huì)議信息,議題信息
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 + '_會(huì)議通知.docx', function (r) {
if (r) {
fs.unlinkSync(filedir + meet_name + '_會(huì)議通知.docx', function (err) {
if (err) throw err;
})
}
/
獲取數(shù)據(jù),處理數(shù)據(jù)
/
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 + ",";//參會(huì)科室
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] != "質(zhì)量管理室" && strArray[k] != "運(yùn)維管理室" && 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' });// 創(chuàng)建行 設(shè)置居中
//pObj4.addText(meet.rows[0].meet_name + '會(huì)議通知', { bold: true, font_face: 'Arial', font_size: 18 });
// 添加文字 設(shè)置字體樣式 加粗 大小
//寫(xiě)入數(shù)據(jù)
var pObj = docx.createP();
pObj.addText('會(huì)議名稱:' ,{bold: true});
pObj.addText(meet.rows[0].meet_name)
var pObj = docx.createP();
pObj.addText('會(huì)議時(shí)間:' ,{bold:true});// 設(shè)置字體顏色
pObj.addText( meet.rows[0].t_begin);
var pObj = docx.createP();
pObj.addText('會(huì)議地點(diǎn):' ,{bold:true});
pObj.addText(meet.rows[0].address);
var pObj = docx.createP();
pObj.addText('參會(huì)科室:' ,{bold:true});
pObj.addText('質(zhì)量管理室、運(yùn)維管理室、稽核資管室'+ st + strs );
var pObj = docx.createP();
pObj.addText('本次會(huì)議內(nèi)容請(qǐng)見(jiàn)下方匯總表,目前已收集到議題【');
pObj.addText("" + result.total + "", { color: 'FF0000' });
pObj.addText('】個(gè)。請(qǐng)議題匯報(bào)科室及涉及參會(huì)科室提前做好準(zhǔn)備。');
var out = fs.createWriteStream(filedir + meet_name + '_會(huì)議通知.docx');// 文件寫(xiě)入
docx.generate(out);// 服務(wù)端生成word
out.on('error', function (err) {
console.log(err);
});
out.on('finish', function () {
console.log("寫(xiě)入完成。");
/*
設(shè)置5分鐘刪除生成的word,避免占用服務(wù)器資源
*/
setTimeout(() => {
try {
fs.access('./public/uploadFile/temporaryFile/' + meet_name + '_會(huì)議通知.docx',
fs.constants.F_OK, (err) => {
if (!err) {
fs.unlinkSync('./public/uploadFile/temporaryFile/' +
meet_name + '_會(huì)議通知.docx');
}
});
} catch (e) {
console.error(e);
}
}, 5 * 60 * 1000);
//返回服務(wù)器中生成的word的路勁
res.send('/uploadFile/temporaryFile/' + meet_name + '_會(huì)議通知.docx');
});
最后:頁(yè)面通過(guò)路徑直接下載word,這里附上頁(yè)面ajax成功后的代碼;
success: function (r) {
$.messager.progress('close');
if (r == "err") {
msgError('導(dǎo)出失敗!,請(qǐng)稍后重試');
}
if (r == "0") {
msgError("請(qǐng)對(duì)會(huì)議基礎(chǔ)信息進(jìn)行完善和確認(rèn)");
} else {
//獲取當(dāng)前網(wǎng)址,如: http://localhost:8080/imgs/doc/abc.jsp
var curWwwPath = window.document.location.href;
//獲取主機(jī)地址之后的目錄,如: imgs/doc/abc.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//獲取主機(jī)地址,如: http://localhost:8080
var localhostPaht = curWwwPath.substring(0, pos);
location.href = localhostPaht + r;
}
},
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。