NodeJS 使用 officegen 生成 Excel, PowerPoint, 和Word文档

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。git

此模块生成Excel(.xlsx),PowerPoint(.pptx)和Word(.docx)文档。 Officegen还支持带有嵌入数据的PowerPoint本机图表对象。github

项目地址: https://github.com/Ziv-Barber/officegen

安装

git clone git://github.com/Ziv-Barber/officegen.gitnpm

npm install officegenpromise

npm install Ziv-Barber/officegen#masterapp

依赖模块

  1. archiver
  2. setimmediate
  3. fast-image-size
  4. xmlbuilder
  5. lodash

API

建立一个文档对象:

var officegen = require('officegen');框架

var myDoc = officegen ( '<type>' );async

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 中使用:ui

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 的例子, 咱们须要本身改造excel

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();
相关文章
相关标签/搜索