一,介紹與需求
1.1,介紹
1, officegen 模塊可以為Microsoft Office 2007及更高版本生成Office Open XML文件。此模塊不依賴于任何框架,您不需要安裝Microsoft Office,因此您可以將它用于任何類型的 JavaScript 應用程序。輸出也是流而不是文件,不依賴于任何輸出工具。此模塊應適用于支持Node.js 0.10或更高版本的任何環境,包括Linux,OSX和Windows。
2, textract 文本提取節點模塊。
3, pdf2json 是一個節點。js模塊解析和轉換PDF從二進制到json格式,它是用PDF構建的。并通過瀏覽器外的交互式表單元素和文本內容解析對其進行擴展。其目標是在web服務中包裝時啟用帶有交互式表單元素的服務器端PDF解析,并在作為命令行實用程序使用時啟用將本地PDF解析為json文件。
1.2,需求
二,文件生成導出
第一步:安裝 officegen
cnpm install officegen --save
第二步:引入officegen
var officegen = require('officegen');var fs = require('fs');var docx = officegen('docx');//wordvar pptx = officegen('pptx');//pptx第三步:使用officegen docx
... 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 tows = ['id', 'provinceZh', 'leaderZh', 'cityZh', 'cityEn'];//創建一個和表頭對應且名稱與數據庫字段對應數據,便于循環取出數據 var pObj = docx.createP({ align: 'center' });// 創建行 設置居中 大標題 pObj.addText('全國所有城市', { bold: true, font_face: 'Arial', font_size: 18 });// 添加文字 設置字體樣式 加粗 大小 // let towsLen = tows.length let dataLen = data.length for (var i = 0; i < dataLen; i++) {//循環數據庫得到的數據,因為取出的數據格式為 //[{"id" : "101010100","provinceZh" : "北京","leaderZh" : "北京","cityZh" : "北京","cityEn" : "beijing"},{…………},{…………}] /************************* 文本 *******************************/ // var pObj = docx.createP();//創建一行 // pObj.addText(`(${i+1}), `,{ bold: true, font_face: 'Arial',}); // pObj.addText(`省級:`,{ bold: true, font_face: 'Arial',}); // pObj.addText(`${data[i]['provinceZh']} `,); // pObj.addText(`市級:`,{ bold: true, font_face: 'Arial',}); // pObj.addText(`${data[i]['leaderZh']} `); // pObj.addText(`縣區:`,{ bold: true, font_face: 'Arial',}); // pObj.addText(`${data[i]['cityZh']}`); /************************* 表格 *******************************/ let SingleRow = [data[i]['id'], data[i]['provinceZh'], data[i]['leaderZh'], data[i]['cityZh']] table.push(SingleRow) } docx.createTable(table, tableStyle); 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.wordprocessingml.document "Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 'Content-disposition': 'attachment; filename=out' + moment(new Date().getTime()).format('YYYYMMDDhhmmss') + '.docx' }); docx.generate(res);// 客戶端導出word
新聞熱點
疑難解答
圖片精選