const officegen = require('officegen') const dbhandle = require('./handle_db') const fs = require('fs') async function generateDocx (params, callback) { let sets = params.sets let table_today = [ [ { val: "频率", opts: { cellColWidth: 150, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "地址", opts: { cellColWidth: 400, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "覆盖范围", opts: { cellColWidth: 300, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "播出时间", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "是否新增", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "类型", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } } ] ] let table_content = [ [ { val: "频率", opts: { cellColWidth: 150, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "地址", opts: { cellColWidth: 400, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "覆盖范围", opts: { cellColWidth: 300, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "播出时间", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "是否新增", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } } ] ] let table_frequency = [ [ { val: "频率", opts: { cellColWidth: 150, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "地址", opts: { cellColWidth: 400, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "覆盖范围", opts: { cellColWidth: 300, b: false, sz: '19', color: "FFFFFF", shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "播出时间", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } }, { val: "是否新增", opts: { cellColWidth: 300, color: "FFFFFF", b: false, sz: '19', shd: { fill: "000000", themeFillTint: "100" }, fontFamily: "SimSun" } } ] ] let tableStyle = { tableColWidth: 4261, borders: true, borderSize: 1, tableSize: 12, tableColor: "ada", tableAlign: "left", tableFontFamily: "SimSun" } let data = [ { type: "text", val: "非法广播监测系统-日报", lopt: { align: 'center' }, opt: { font_size: 20, font_face: 'Microsoft YaHei', } }, [ { indentFirstLine: 500 }, { type: "text", val: `当日(${params.time})监测到`, opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "text", val: `${sets.Illegal}`, opt: { font_size: 12, font_face: 'Microsoft YaHei', bold: true, underline: true } }, { type: "text", val: `个疑似非法广播,其中新增疑似非法广播`, opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "text", val: `${sets.Illegal_new}`, opt: { font_size: 12, font_face: 'Microsoft YaHei', bold: true, underline: true } }, { type: "text", val: `个,内容非法广播`, opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "text", val: `${sets.Illegal_content}`, opt: { font_size: 12, font_face: 'Microsoft YaHei', bold: true, underline: true } }, { type: "text", val: `个,频率非法广播`, opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "text", val: `${sets.Illegal_frequency}`, opt: { font_size: 12, font_face: 'Microsoft YaHei', bold: true, underline: true, } }, { type: "text", val: `个。`, opt: { font_size: 12, font_face: 'Microsoft YaHei' } } ], { type: "text", val: '(1)当日监测到的非法广播:', opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "table", val: table_today, opt: tableStyle }, { type: "text", val: '(2)当日监听到的内容非法广播清单:', opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "table", val: table_content, opt: tableStyle }, { type: "text", val: '(3)当日监听到的频率非法广播清单:', opt: { font_size: 12, font_face: 'Microsoft YaHei' } }, { type: "table", val: table_frequency, opt: tableStyle } ] if (params.a.length == 0) { table_today.push(['', '', '', '', '', '']) } else { for (let k = 0; k < params.a.length; k++) { table_today.push(params.a[k]) } } if (params.b.length == 0) { table_content.push(['', '', '', '', '']) } else { for (let k = 0; k < params.b.length; k++) { table_content.push(params.b[k]) } } if (params.c.length == 0) { table_frequency.push(['', '', '', '', '']) } else { for (let k = 0; k < params.c.length; k++) { table_frequency.push(params.c[k]) } } /** * starting generate word */ let stamp = dbhandle.date_format(new Date(), 'YYMMDDhhmmss') let path = `public/docx/${stamp}_day.docx` let send = function () { return new Promise(function (resolve, reject) { console.log(__dirname) let docx = officegen('docx') docx.createByJson(data); let out = fs.createWriteStream(path) // fs calling this function to report errors: out.on('error', function (err) { resolve({ message: '路径错误,无法生成文件。', file_path: null }) }) // Officegen calling this function to report errors: docx.on('error', function (err) { resolve({ message: 'WORD文档生成失败。', file_path: null }) }) //Officegen calling this function after finishing to generate the docx document: docx.on('finalize', function (written) { console.log(written) resolve({ message: 'WORD文档生成成功。', file_path: `docx/${stamp}_day.docx` }) }) // Async call to generate the output file: docx.generate(out) }) } callback(await send(), params) } module.exports = { generateDocx }