NodeJS 使用 officegen 生成 Excel, PowerPoint, 和Word文档
2018-09-04 14:33
2331 查看
NodeJS 使用 officegen 生成 Excel(.xlsx),PowerPoint(.pptx)和Word(.docx)文档
officegen 模块可以为Microsoft Office 2007及更高版本生成Office Open XML文件。此模块不依赖于任何框架,您不需要安装Microsoft Office,因此您可以将它用于任何类型的JavaScript应用程序。输出也是流而不是文件,不依赖于任何输出工具。此模块应适用于支持Node.js 0.10或更高版本的任何环境,包括Linux,OSX和Windows。此模块生成Excel(.xlsx),PowerPoint(.pptx)和Word(.docx)文档。 Officegen还支持带有嵌入数据的PowerPoint本机图表对象。
项目地址: https://github.com/Ziv-Barber/officegen
安装
git clone git://github.com/Ziv-Barber/officegen.git
npm install officegen
npm install Ziv-Barber/officegen#master
依赖模块
archiversetimmediate
fast-image-size
xmlbuilder
lodash
API
创建一个文档对象:
var officegen = require('officegen');
var myDoc = officegen ( '<type>' );
type 可以是 pptx, docx, xlsx
var myDoc = officegen ({ 'type': '<type of document to create>', // 类型 {} // options 配置项 });
监听文档完成或者错误
// 监听文档完成 myDoc.on ( 'finalize', function ( written ) { console.log ( written ); });
// 监听文档错误 myDoc.on ( 'error', function ( err ) { console.log ( err ); });
// 同样可以在实例化的时候指定 var pptx = officegen ({ 'type': 'pptx', // or 'xlsx', etc 'onend': function ( written ) { console.log(written); }, 'onerr': function ( err ) { console.log ( err ); } });
// 通样可以在生成文件的时候指定 var myDoc = officegen('pptx'); var out = fs.createWriteStream ( 'out.pptx' ); // 创建文件 myDoc.generate ( out, { 'finalize': function ( written ) { console.log ( written ); }, 'error': function ( err ) { console.log ( err ); } });
基于 http 流创建文件
var http = require("http"); var officegen = require('officegen'); http.createServer ( function ( request, response ) { response.writeHead ( 200, { "Content-Type": "application/vnd.openxmlformats-officedocument.presentationml.presentation", 'Content-disposition': 'attachment; filename=surprise.pptx' }); var pptx = officegen ( 'pptx' ); pptx.on ( 'finalize', function ( written ) { // ... }); pptx.on ( 'error', function ( err ) { // ... }); // ... (fill pptx with data) pptx.generate ( response ); }).listen ( 3000 );
将数据放在文档对象中:
更改文档标题(pptx,ppsx,docx):
var pptx = officegen ({ 'type': 'pptx', 'title': '<title>' }); // or pptx.setDocTitle ( '<title>' );
以下只有在 word 中使用:
var docx = officegen ({ 'type': 'docx', 'subject': '...', 'keywords': '...', 'description': '...' }); // or docx.setDocSubject ( '...' ); docx.setDocKeywords ( '...' ); docx.setDescription ( '...' );
生成 ppt https://github.com/Ziv-Barber/officegen#powerpoint 参考
生成 word https://github.com/Ziv-Barber/officegen#word 参考
生成 excel https://github.com/Ziv-Barber/officegen#excel 参考
实例: https://github.com/Ziv-Barber/officegen#examples
使用 promise
官网中没有 promise 的例子, 我们需要自己改造async function generate(){ return new Promise((resolve, reject) => { var myDoc = officegen('pptx'); dosoming ... var out = fs.createWriteStream ( 'out.pptx' ); // 创建文件 myDoc.generate ( out, { 'finalize': function(data){ console.log(data); }, 'error': reject, }); out.on('finish', function(){ resolve(true); }); }); }
// 调用 let resuslt = await generate();
相关文章推荐
- 使用MSOffice .NET API 将文档(Word \ Excel \ PowerPoint \ Visio \ text \ XML \ RTF \ CSV等)转换为PDF
- nodejs(officegen)+vue(axios)在客户端导出word文档的方法
- 这几天我写了一个类库,可以提供office编程时使用Word, Excel,PowerPoint等的保存后事件DocumentAfterSave
- 将office文档(word,excel,powerpoint)转换为pdf
- 使用Interop.Excel生成Word表格文档
- 如何安装 Microsoft Office 兼容包,以便能够使用早期版本的 Excel、PowerPoint 和 Word 来打开和保存来自 2007 Office 程序的文件
- 将office文档(word,excel,powerpoint)转换为pdf
- 在VB.NET窗体中如何打开/查看Office文档(Word, Excel, PowerPoint)
- JSP生成WORD文档,EXCEL文档,PDF文档
- 使用Spring MVC生成Excel文档
- JSP生成WORD文档,EXCEL文档,PDF文档
- ASP生成WORD文档或EXCEL文档
- 使用Delphi 7控制Word 2000生成文档的方法
- 用ASP生成Word、Excel和Txt文档
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- 使用POI生成Excel文档并设置打印样式
- JSP生成WORD文档,EXCEL文档,PDF文档
- 使用VS.NET生成word文档并直接下载
- 在JAVA项目中,使用JasperReports与iReport,直接连接打印机可打印(word excel xml pdf )文档