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

栏目: Node.js · 发布时间: 7年前

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

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

  anziguoer 发布于 今天 14:33

字数 591

阅读 1

收藏 0

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

依赖模块

  1. archiver
  2. setimmediate
  3. fast-image-size
  4. xmlbuilder
  5. 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();

© 著作权归作者所有

共有人打赏支持

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

anziguoer

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

参与源创会

“源创会”在线下联结了各位 OSCer,推广开源项目和理念,很荣幸有你的参与~

领取条件:参与过开源中国“源创会”的 OSCer 可以领取

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

十周年

开源中国十周岁啦~ 感谢 OSCer 一路同行

领取条件:领取开源报告并收获三位 OSCer 点亮即可领取

粉丝 27

博文 80

码字总数 39011

作品 0

海淀

程序员

相关文章 最新文章

nodejs officegen doc table 没办法生成表格边框吗

用nodejs生成并导出word,找了下最好用的是officegen模块,可是在生成table表单的时候,没办法设置边框,table没有边框,请教有没有人用过,能不能生成边框,或者推荐其他的模板。...

带着阳

2016/08/03

74

0

使用 Apache POI 和 OpenOffice API 在 Linux 中统计 Office 文档的页数

简介: 在实际的项目开发中经常会遇到需要在不同的操作系统平台上统计 Microsoft Office 系列文档页数的要求。Apache POI 提供了一套完整的用于访问微软格式文档的 Java API。但是 Apache P...

IBMdW

2012/10/26

1K

0

基于Visual C++2010 与office2010开发办公自动化(2)-自动生成excel与word并打开

VS2010是新一代全新开发 工具 属于全新的系统构架 VS2010旗舰版功能全景 Office 2010,是微软推出新一代办公软件,开发代号为Office 14,实际是第12个发行版。该软件共有6个版本,分别是初级版...

junwong

2012/03/09

0

0

基于Visual C++2010 与office2010开发办公自动化(25)-如何使用自动化生成Excel图表

VS2010新特性: Office Word 2010 Office Word 2010 增强了 Navigation Pane 特性,用户可在 Navigation Pane 中快速切换至任何一章节的开头(根据标题样式判断),同时也可在输入框中进行即...

junwong

2012/03/09

0

0

受支持的PDF文档格式该怎样进行转换

ABBYY PDF Transformer+图文识别软件,是一款可创建、编辑、添加注释以及将PDF文件转换为其他可编辑格式的通用工具,在创建或转换PDF时支持很多PDF文档格式,是日常办公的好帮手,大大提高了...

ABBYY

2016/01/13

24

0

没有更多内容

加载失败,请刷新页面

加载更多
Spring IOC 启动过程

1. 引言 本篇博文主要介绍 IOC 容器的启动过程,启动过程分为两个步骤,第一个阶段是容器的启动阶段,第二个阶段是 Bean 实例化阶段,这两个阶段各自需要执行的步骤如下图,接下来会一一介绍...

firepation

20分钟前

1

0

咕泡-观察者 observer 设计模式笔记

观察者模式(Observer) 应用场景:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新 Spring 中Observer 模式常用的地方是Listene...

职业搬砖20年

21分钟前

1

0

原 荐 NodeJS 使用 officegen 生成 Excel, PowerPoint, 和Word文档
Solidity智能合约编程漏洞及对策

上溢(Overflow)和下溢(Underflow) Solidity能处理256位的整数。所以 2²⁵⁶-1 加1就会为0.这个就是Overflow 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF+ 0x00000000000000000000000000...

全部原谅

25分钟前

1

0

centos系统创建文件夹目录显示颜色

linux系统默认目录颜色是蓝色的,在黑背景下看不清楚,可以通过以下2种方法修改ls查看的颜色。 方法: 1、拷贝/etc/DIR_COLORS文件为当前主目录的 .dir_colors cp /etc/DIR_COLORS ~/.dir_co...

问题终结者

27分钟前

1

0

原 荐 NodeJS 使用 officegen 生成 Excel, PowerPoint, 和Word文档
MicroPython拼插编程实例:点亮心形8x8点阵

一、什么是TPYBoard开发板 TPYBoard是以遵照MIT许可的MicroPython为基础的一款MicroPython开发板,它基于STM32F405单片机,通过USB接口进行数据传输。该开发板内置4个LED灯、一个加速传感器,...

bodasisiter

30分钟前

1

0

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

没有更多内容

加载失败,请刷新页面

加载更多

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Release It!

Release It!

Michael T. Nygard / Pragmatic Bookshelf / 2007-03-30 / USD 34.95

“Feature complete” is not the same as “production ready.” Whether it’s in Java, .NET, or Ruby on Rails, getting your application ready to ship is only half the battle. Did you design your system to......一起来看看 《Release It!》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码