【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
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
依赖模块
- archiver
- setimmediate
- 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();
来源:oschina
链接:https://my.oschina.net/u/1402253/blog/1942214