使用了 officegen 第三方 api 實現生成word 文檔,可是發現下劃線沒法知足需求。修改源碼以便後人踩坑,如下是個人一個demo,經過傳參數設置下劃線顏色以及高度。本demo 經過json 配置方式,還有api 調用方式實現方式,詳細能夠參考git 的例子 GITHUB Api地址: https://github.com/Ziv-Barber/officegen var officegen = require('officegen'); var fs = require('fs'); var path = require('path'); var docx = officegen('docx'); var header = docx.getHeader().createP({ align: ('center') }); console.log('路徑為' + __dirname); var table = [ [{ val: 'No.', opts: { cellColWidth: 4261, b: true, sz: '48', shd: { fill: '7F7F7F', themeFill: 'text1', themeFillTint: '80' }, fontFamily: 'Avenir Book' } }, { val: 'Title1', opts: { b: true, color: 'A00000', align: 'right', shd: { fill: '92CDDC', themeFill: 'text1', themeFillTint: '80' } } }, { val: 'Title2', opts: { align: 'center', cellColWidth: 42, b: true, sz: '48', shd: { fill: '92CDDC', themeFill: 'text1', themeFillTint: '80' } } }], [1, 'All grown-ups were once children', ''], [2, 'there is no harm in putting off a piece of work until another day.', ''], [3, 'But when it is a matter of baobabs, that always means a catastrophe.', ''], [4, 'watch out for the baobabs!', 'END'] ] var tableStyle = { tableColWidth: 4261, tableSize: 24, tableColor: 'ada', tableAlign: 'left', tableFontFamily: 'Comic Sans MS' } var data = [ [{ align: 'right' }, { type: 'text', val: 'Simple' }, { type: 'text', val: ' with color', opt: { color: '000088' } }, { type: 'text', val: ' and back color.', opt: { color: '00ffff', back: '000088' } }, { type: 'linebreak' }, { type: 'text', val: 'Bold + underline', opt: { bold: true, underline: true } } ], { type: 'horizontalline', opt: { height: "2", fillcolor: 'FF0000' } }, [ { type: 'text', val: ' backline text1.', opt: { bold: true } }, { type: 'text', val: ' backline text2.', opt: { color: '000088' } } ], { type: 'text', val: 'Left this text.', lopt: { align: 'left' } }, { type: 'text', val: 'Center this text.', lopt: { align: 'center' } }, { type: 'text', val: 'Right this text.', lopt: { align: 'right' } }, { type: 'text', val: 'Fonts face only.', opt: { font_face: 'Arial' } }, { type: 'text', val: 'Fonts face and size.', opt: { font_face: 'Arial', font_size: 40 } }, { type: 'table', val: table, opt: tableStyle }, { type: 'pagebreak' }, [{}, { type: 'numlist' }, { type: 'text', val: 'numList1.' }, { type: 'numlist' }, { type: 'text', val: 'numList2.' } ], [{}, { type: 'dotlist' }, { type: 'text', val: 'dotlist1.' }, { type: 'dotlist' }, { type: 'text', val: 'dotlist2.' } ], { type: 'pagebreak' } ] docx.createByJson(data) //var pObj = docx.createTable(table, tableStyle); var out = fs.createWriteStream('out.docx'); // 文件寫入 out.on('error', function(err) { console.log(err); }); var result = docx.generate(out);
找到源碼修改git
修改代碼部分以下:gendocx.js 部分github
else if ( objs_list[i].data[j].horizontal_line ) { console.log(objs_list[i].data[j]); var height=typeof objs_list[i].data[j].options.height=="string"?objs_list[i].data[j].options.height:".75"; var fillcolor=typeof objs_list[i].data[j].options.fillcolor=="string"?objs_list[i].data[j].options.fillcolor:"e0e0e0"; //outString += `<w:r><w:pict><v:rect o:spt="1" style="width:0;height:${height}pt" o:hralign="center" o:hrstd="t" o:hr="t" fillcolor="#${fillcolor}" stroked="f"/></w:pict></w:r>`; outString += `<w:r><w:pict><v:rect o:spt="1" style="height:${height}pt;width:0pt;" fillcolor="#${fillcolor}" filled="t" stroked="f" coordsize="21600,21600" o:hr="t" o:hrstd="t" o:hrnoshade="t" o:hralign="center"/></w:pict></w:r>`; // Bookmark start support: }
docx-p.js 部分json
MakeDocxP.prototype.addHorizontalLine = function (opt) { var newP = this; newP.data[newP.data.length] = { 'horizontal_line': true, options:opt||{} }; };
修改成傳參數設置其下滑線顏色跟高度最後附上效果圖:api