ExcelJS 使用帮助文档

ExcelJS 使用帮助文档

个人备忘,原文地址:https://github.com/exceljs/exceljs/blob/master/README_zh.md

安装

npm install exceljs

新的功能!

  • 重大版本更改-主要的ExcelJS接口已从基于流的API迁移到异步迭代器,从而使代码更简洁。 虽然技术上是一个突破性的变化,但大多数API都没有变化,详细信息请参见UPGRADE-4.0.md。
  • 此升级来自以下合并:
    • [MAJOR VERSION] Async iterators #1135
    • [MAJOR VERSION] Move node v8 support to ES5 imports #1142
    团队做了很多工作-特别是 Andreas Lubbe and Siemienik Paweł.

贡献

欢迎贡献!这可以帮助我了解大家需要一些什么功能,或者哪些 bugs 造成了极大的麻烦。

我只有一个请求;如果您提交对错误修复的请求(PR),请添加一个能够解决问题的单元测试或集成测试(在 spec 文件夹中)。
即使只是测试失败的请求(PR)也可以 - 我可以分析测试的过程并以此修复代码。

注意:请尽可能避免在请求(PR)中修改软件包版本。
版本一般在发布时会进行更新,任何版本更改很可能导致合并冲突。

明确地说,添加到该库的所有贡献都将包含在该库的 MIT 许可证中。

目录

  • 导入
  • 接口
    • 创建工作簿
    • 设置工作簿属性
    • 工作簿视图
    • 添加工作表
    • 删除工作表
    • 访问工作表
    • 工作表状态
    • 工作表属性
    • 页面设置
    • 页眉和页脚
    • 工作表视图
      • 冻结视图
      • 拆分视图
    • 自动筛选器
      • Add Rows
      • 处理单个单元格
      • 合并单元格
      • Insert Rows
      • Splice
      • 重复行
    • 定义名称
    • 数据验证
    • 单元格注释
    • 表格
    • 样式
      • 数字格式
      • 字体
      • 对齐
      • 边框
      • 填充
      • 富文本
    • 条件格式化
    • 大纲级别
    • 图片
    • 工作表保护
    • 文件 I/O
      • XLSX
        • 读 XLSX
        • 写 XLSX
      • CSV
        • 读 CSV
        • 写 CSV
      • 流式 I/O
        • 流式 XLSX
  • 浏览器
  • 值类型
    • Null 值
    • 合并单元格
    • 数字值
    • 字符串值
    • 日期值
    • 超链接值
    • 公式值
      • 共享公式
      • 公式类型
      • 数组公式
    • 富文本值
    • 布尔值
    • 错误值
  • 配置
  • 已知的问题
  • 发布历史

导入⬆

const ExcelJS = require('exceljs');

ES5 导入⬆

要使用 ES5 编译代码,请使用 dist/es5 路径。

const ExcelJS = require('exceljs/dist/es5');

**注意:**ES5 版本对许多 polyfill 都具有隐式依赖,而 exceljs 不再明确添加。
您需要在依赖项中添加 core-jsregenerator-runtime,并在导入 exceljs 之前在代码中包含以下引用:

// exceljs 所需的 polyfills
require('core-js/modules/es.promise');
require('core-js/modules/es.string.includes');
require('core-js/modules/es.object.assign');
require('core-js/modules/es.object.keys');
require('core-js/modules/es.symbol');
require('core-js/modules/es.symbol.async-iterator');
require('regenerator-runtime/runtime');

const ExcelJS = require('exceljs/dist/es5');

对于 IE 11,您还需要一个 polyfill 以支持 unicode regex 模式。 例如,

const rewritePattern = require('regexpu-core');
const {generateRegexpuOptions} = require('@babel/helper-create-regexp-features-plugin/lib/util');

const {RegExp} = global;
try {
  new RegExp('a', 'u');
} catch (err) {
  global.RegExp = function(pattern, flags) {
    if (flags && flags.includes('u')) {
      return new RegExp(rewritePattern(pattern, flags, generateRegexpuOptions({flags, pattern})));
    }
    return new RegExp(pattern, flags);
  };
  global.RegExp.prototype = RegExp;
}

浏览器端⬆

ExcelJS 在 dist/ 文件夹内发布了两个支持浏览器的包:

一个是隐式依赖 core-js polyfills 的…

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.js">script>
<script src="exceljs.js">script>

另一个则没有…

<script src="--your-project's-pollyfills-here--">script>
<script src="exceljs.bare.js">script>

接口⬆

创建工作簿⬆

const workbook = new ExcelJS.Workbook();

设置工作簿属性⬆

workbook.creator = 'Me';
workbook.lastModifiedBy = 'Her';
workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
workbook.lastPrinted = new Date(2016, 9, 27);
// 将工作簿日期设置为 1904 年日期系统
workbook.properties.date1904 = true;

设置计算属性⬆

// 在加载时强制工作簿计算属性
workbook.calcProperties.fullCalcOnLoad = true;

工作簿视图⬆

工作簿视图控制在查看工作簿时 Excel 将打开多少个单独的窗口。

workbook.views = [
  {
    x: 0, y: 0, width: 10000, height: 20000,
    firstSheet: 0, activeTab: 1, visibility: 'visible'
  }
]

添加工作表⬆

const sheet = workbook.addWorksheet('My Sheet');

使用 addWorksheet 函数的第二个参数来指定工作表的选项。

示例:

// 创建带有红色标签颜色的工作表
const sheet = workbook.addWorksheet('My Sheet', {properties:{tabColor:{argb:'FFC0000'}}});

// 创建一个隐藏了网格线的工作表
const sheet = workbook.addWorksheet('My Sheet', {views: [{showGridLines: false}]});

// 创建一个第一行和列冻结的工作表
const sheet = workbook.addWorksheet('My Sheet', {views:[{xSplit: 1, ySplit:1}]});

// 使用A4设置的页面设置设置创建新工作表 - 横向
const worksheet =  workbook.addWorksheet('My Sheet', {
  pageSetup:{paperSize: 9, orientation:'landscape'}
});

// 创建一个具有页眉页脚的工作表
const sheet = workbook.addWorksheet('My Sheet', {
  headerFooter:{firstHeader: "Hello Exceljs", firstFooter: "Hello World"}
});

// 创建一个冻结了第一行和第一列的工作表
const sheet = workbook.addWorksheet('My Sheet', {views:[{state: 'frozen', xSplit: 1, ySplit:1}]});

删除工作表⬆

使用工作表的 id 从工作簿中删除工作表。

示例:

// 创建工作表
const sheet = workbook.addWorksheet('My Sheet');

// 使用工作表 id 删除工作表
workbook.removeWorksheet(sheet.id)

访问工作表⬆

// 遍历所有工作表
// 注意: workbook.worksheets.forEach 仍然是可以正常运行的, 但是以下的方式更好
workbook.eachSheet(function(worksheet, sheetId) {
  // ...
});

// 按 name 提取工作表
const worksheet = workbook.getWorksheet('My Sheet');

// 按 id 提取工作表
const worksheet = workbook.getWorksheet(1);

工作表状态⬆

// 使工作表可见
worksheet.state = 'visible';

// 隐藏工作表
worksheet.state = 'hidden';

// 从“隐藏/取消隐藏”对话框中隐藏工作表
worksheet.state = 'veryHidden';

工作表属性⬆

工作表支持属性存储,以允许控制工作表的某些功能。

// 创建具有属性的新工作表
const worksheet = workbook.addWorksheet('sheet', {properties:{tabColor:{argb:'FF00FF00'}}});

// 创建一个具有属性的可写的新工作表
const worksheetWriter = workbookWriter.addWorksheet('sheet', {properties:{outlineLevelCol:1}});

// 之后调整属性(工作表读写器不支持该操作)
worksheet.properties.outlineLevelCol = 2;
worksheet.properties.defaultRowHeight = 15;

支持的属性

属性名 默认值 描述
tabColor undefined 标签的颜色
outlineLevelCol 0 工作表列大纲级别
outlineLevelRow 0 工作表行大纲级别
defaultRowHeight 15 默认行高
defaultColWidth (optional) 默认列宽
dyDescent 55 TBD

工作表尺寸⬆

一些新的尺寸属性已添加到工作表中…

属性名 描述
rowCount 文档的总行数。 等于具有值的最后一行的行号。
actualRowCount 具有值的行数的计数。 如果中间文档行为空,则该行将不包括在计数中。
columnCount 文档的总列数。 等于所有行的最大单元数。
actualColumnCount 具有值的列数的计数。

页面设置⬆

所有可能影响工作表打印的属性都保存在工作表上的 pageSetup 对象中。

// 使用 A4 横向的页面设置创建新工作表
const worksheet =  workbook.addWorksheet('sheet', {
  pageSetup:{paperSize: 9, orientation:'landscape'}
});

// 使用适合页面的pageSetup设置创建一个新的工作表编写器
const worksheetWriter = workbookWriter.addWorksheet('sheet', {
  pageSetup:{fitToPage: true, fitToHeight: 5, fitToWidth: 7}
});

// 之后调整页面设置配置
worksheet.pageSetup.margins = {
  left: 0.7, right: 0.7,
  top: 0.75, bottom: 0.75,
  header: 0.3, footer: 0.3
};

// 设置工作表的打印区域
worksheet.pageSetup.printArea = 'A1:G20';

// 通过使用 `&&` 分隔打印区域来设置多个打印区域
worksheet.pageSetup.printArea = 'A1:G10&&A11:G20';

// 在每个打印页面上重复特定的行
worksheet.pageSetup.printTitlesRow = '1:3';

// 在每个打印页面上重复特定列
worksheet.pageSetup.printTitlesColumn = 'A:C';

支持的页面设置配置项

属性名 默认值 描述
margins 页面上的空白边距。 单位为英寸。
orientation 'portrait' 页面方向 - 即较高 ('portrait') 或者较宽 ('landscape')
horizontalDpi 4294967295 水平方向上的 DPI。默认值为 -1
verticalDpi 4294967295 垂直方向上的 DPI。默认值为 -1
fitToPage 是否使用 fitToWidthfitToHeightscale 设置。默认基于存在于 pageSetup 对象中的设置-如果两者都存在,则 scale 优先级高(默认值为 false)。
pageOrder 'downThenOver' 打印页面的顺序-['downThenOver', 'overThenDown'] 之一
blackAndWhite false 无色打印
draft false 打印质量较低(墨水)
cellComments 'None' 在何处放置批注-['atEnd','asDisplayed','None']中的一个
errors 'displayed' 哪里显示错误 -['dash', 'blank', 'NA', 'displayed'] 之一
scale 100 增加或减小打印尺寸的百分比值。 当 fitToPagefalse 时激活
fitToWidth 1 纸张应打印多少页宽。 当 fitToPagetrue 时激活
fitToHeight 1 纸张应打印多少页高。 当 fitToPagetrue 时激活
paperSize 使用哪种纸张尺寸(见下文)
showRowColHeaders false 是否显示行号和列字母
showGridLines false 是否显示网格线
firstPageNumber 第一页使用哪个页码
horizontalCentered false 是否将工作表数据水平居中
verticalCentered false 是否将工作表数据垂直居中

示例纸张尺寸

属性名
Letter undefined
Legal 5
Executive 7
A3 8
A4 9
A5 11
B5 (JIS) 13
Envelope #10 20
Envelope DL 27
Envelope C5 28
Envelope B5 34
Envelope Monarch 37
Double Japan Postcard Rotated 82
16K 197x273 mm 119

页眉和页脚⬆

这是添加页眉和页脚的方法。
添加的内容主要是文本,例如时间,简介,文件信息等,您可以设置文本的样式。
此外,您可以为首页和偶数页设置不同的文本。

注意:目前不支持图片。

// 创建一个带有页眉和页脚的工作表
var sheet = workbook.addWorksheet('My Sheet', {
  headerFooter:{firstHeader: "Hello Exceljs", firstFooter: "Hello World"}
});

// 创建一个带有页眉和页脚可写的工作表
var worksheetWriter = workbookWriter.addWorksheet('sheet', {
  headerFooter:{firstHeader: "Hello Exceljs", firstFooter: "Hello World"}
});
// 代码中出现的&开头字符对应变量,相关信息可查阅下文的变量表
// 设置页脚(默认居中),结果:“第 2 页,共 16 页”
worksheet.headerFooter.oddFooter = "第 &P 页,共 &N 页";

// 将页脚(默认居中)设置为粗体,结果是:“第2页,共16页”
worksheet.headerFooter.oddFooter = "Page &P of &N";

// 将左页脚设置为 18px 并以斜体显示。 结果:“第2页,共16页”
worksheet.headerFooter.oddFooter = "&LPage &P of &N";

// 将中间标题设置为灰色Aril,结果为:“ 52 exceljs”
worksheet.headerFooter.oddHeader = "&C&KCCCCCC&\"Aril\"52 exceljs";

// 设置页脚的左,中和右文本。 结果:页脚左侧为“ Exceljs”。 页脚中心的“ demo.xlsx”。 页脚右侧的“第2页”
worksheet.headerFooter.oddFooter = "&Lexceljs&C&F&RPage &P";

// 在首页添加不同的页眉和页脚
worksheet.headerFooter.differentFirst = true;
worksheet.headerFooter.firstHeader = "Hello Exceljs";
worksheet.headerFooter.firstFooter = "Hello World"

支持的 headerFooter 设置

属性名 默认值 描述
differentFirst false differentFirst 的值设置为 true,这表示第一页的页眉/页脚与其他页不同
differentOddEven false differentOddEven 的值设置为 true,表示奇数页和偶数页的页眉/页脚不同
oddHeader null 设置奇数(默认)页面的标题字符串,可以设置格式化字符串
oddFooter null 设置奇数(默认)页面的页脚字符串,可以设置格式化字符串
evenHeader null 设置偶数页的标题字符串,可以设置格式化字符串
evenFooter null 为偶数页设置页脚字符串,可以设置格式化字符串
firstHeader null 设置首页的标题字符串,可以设置格式化字符串
firstFooter null 设置首页的页脚字符串,可以设置格式化字符串

脚本命令

命令 描述
&L 将位置设定在左侧
&C 将位置设置在中心
&R 将位置设定在右边
&P 当前页码
&N 总页数
&D 当前日期
&T 当前时间
&G 照片
&A 工作表名称
&F 文件名称
&B 加粗文本
&I 斜体文本
&U 文本下划线
&“font name” 字体名称,例如&“ Aril”
&font size 字体大小,例如12
&KHEXCode 字体颜色,例如 &KCCCCCC

工作表视图⬆

现在,工作表支持视图列表,这些视图控制Excel如何显示工作表:

  • frozen - 顶部和左侧的许多行和列被冻结在适当的位置。 仅右下部分会滚动
  • split - 该视图分为4个部分,每个部分可半独立滚动。

每个视图还支持各种属性:

属性名 默认值 描述
state 'normal' 控制视图状态 - 'normal', 'frozen' 或者 'split' 之一
rightToLeft false 将工作表视图的方向设置为从右到左
activeCell undefined 当前选择的单元格
showRuler true 在页面布局中显示或隐藏标尺
showRowColHeaders true 显示或隐藏行标题和列标题(例如,顶部的 A1,B1 和左侧的1,2,3)
showGridLines true 显示或隐藏网格线(针对未定义边框的单元格显示)
zoomScale 100 用于视图的缩放比例
zoomScaleNormal 100 正常缩放视图
style undefined 演示样式- pageBreakPreviewpageLayout 之一。 注意:页面布局与 frozen 视图不兼容

冻结视图⬆

冻结视图支持以下额外属性:

属性名 默认值 描述
xSplit 0 冻结多少列。要仅冻结行,请将其设置为 0undefined
ySplit 0 冻结多少行。要仅冻结列,请将其设置为 0undefined
topLeftCell special 哪个单元格将在右下窗格中的左上角。注意:不能是冻结单元格。默认为第一个未冻结的单元格
worksheet.views = [
  {state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'G10', activeCell: 'A1'}
];

拆分视图⬆

拆分视图支持以下额外属性:

属性名 默认值 描述
xSplit 0 从左侧多少个点起,以放置拆分器。要垂直拆分,请将其设置为 0undefined
ySplit 0 从顶部多少个点起,放置拆分器。要水平拆分,请将其设置为 0undefined
topLeftCell undefined 哪个单元格将在右下窗格中的左上角。
activePane undefined 哪个窗格将处于活动状态-topLefttopRightbottomLeftbottomRight 中的一个
worksheet.views = [
  {state: 'split', xSplit: 2000, ySplit: 3000, topLeftCell: 'G10', activeCell: 'A1'}
];

自动筛选器⬆

可以对工作表应用自动筛选器。

worksheet.autoFilter = 'A1:C1';

尽管范围字符串是 autoFilter 的标准形式,但工作表还将支持以下值:

// 将自动筛选器设置为从 A1 到 C1
worksheet.autoFilter = {
  from: 'A1',
  to: 'C1',
}

// 将自动筛选器设置为从第3行第1列的单元格到第5行第12列的单元格
worksheet.autoFilter = {
  from: {
    row: 3,
    column: 1
  },
  to: {
    row: 5,
    column: 12
  }
}

// 将自动筛选器设置为从D3到第7行第5列中的单元格
worksheet.autoFilter = {
  from: 'D3',
  to: {
    row: 7,
    column: 5
  }
}

列⬆

// 添加列标题并定义列键和宽度
// 注意:这些列结构仅是构建工作簿的方便之处,除了列宽之外,它们不会完全保留。
worksheet.columns = [
  { header: 'Id', key: 'id', width: 10 },
  { header: 'Name', key: 'name', width: 32 },
  { header: 'D.O.B.', key: 'DOB', width: 10, outlineLevel: 1 }
];

// 通过键,字母和基于1的列号访问单个列
const idCol = worksheet.getColumn('id');
const nameCol = worksheet.getColumn('B');
const dobCol = worksheet.getColumn(3);

// 设置列属性

// 注意:将覆盖 C1 单元格值
dobCol.header = 'Date of Birth';

// 注意:这将覆盖 C1:C2 单元格值
dobCol.header = ['Date of Birth', 'A.K.A. D.O.B.'];

// 从现在开始,此列将以 “dob” 而不是 “DOB” 建立索引
dobCol.key = 'dob';

dobCol.width = 15;

// 如果需要,隐藏列
dobCol.hidden = true;

// 为列设置大纲级别
worksheet.getColumn(4).outlineLevel = 0;
worksheet.getColumn(5).outlineLevel = 1;

// 列支持一个只读字段,以指示基于 `OutlineLevel` 的折叠状态
expect(worksheet.getColumn(4).collapsed).to.equal(false);
expect(worksheet.getColumn(5).collapsed).to.equal(true);

// 遍历此列中的所有当前单元格
dobCol.eachCell(function(cell, rowNumber) {
  // ...
});

// 遍历此列中的所有当前单元格,包括空单元格
dobCol.eachCell({ includeEmpty: true }, function(cell, rowNumber) {
  // ...
});

// 添加一列新值
worksheet.getColumn(6).values = [1,2,3,4,5];

// 添加稀疏列值
worksheet.getColumn(7).values = [,,2,3,,5,,7,,,,11];

// 剪切一列或多列(右边的列向左移动)
// 如果定义了列属性,则会相应地对其进行切割或移动
// 已知问题:如果拼接导致任何合并的单元格移动,结果可能是不可预测的
worksheet.spliceColumns(3,2);

// 删除一列,再插入两列。
// 注意:第4列及以上的列将右移1列。
// 另外:如果工作表中的行数多于列插入项中的值,则行将仍然被插入,就好像值存在一样。
const newCol3Values = [1,2,3,4,5];
const newCol4Values = ['one', 'two', 'three', 'four', 'five'];
worksheet.spliceColumns(3, 1, newCol3Values, newCol4Values);

行⬆

// 获取一个行对象。如果尚不存在,则将返回一个新的空对象
const row = worksheet.getRow(5);

// Get multiple row objects. If it doesn't already exist, new empty ones will be returned
const rows = worksheet.getRows(5, 2); // start, length (>0, else undefined is returned)

// 获取工作表中的最后一个可编辑行(如果没有,则为 `undefined`)
const row = worksheet.lastRow;

// 设置特定的行高
row.height = 42.5;

// 隐藏行
row.hidden = true;

// 为行设置大纲级别
worksheet.getRow(4).outlineLevel = 0;
worksheet.getRow(5).outlineLevel = 1;

// 行支持一个只读字段,以指示基于 `OutlineLevel` 的折叠状态
expect(worksheet.getRow(4).collapsed).to.equal(false);
expect(worksheet.getRow(5).collapsed).to.equal(true);


row.getCell(1).value = 5; // A5 的值设置为5
row.getCell('name').value = 'Zeb'; // B5 的值设置为 “Zeb” - 假设第2列仍按名称键入
row.getCell('C').value = new Date(); // C5 的值设置为当前时间

// 获取行并作为稀疏数组返回
// 注意:接口更改:worksheet.getRow(4) ==> worksheet.getRow(4).values
row = worksheet.getRow(4).values;
expect(row[5]).toEqual('Kyle');

// 通过连续数组分配行值(其中数组元素 0 具有值)
row.values = [1,2,3];
expect(row.getCell(1).value).toEqual(1);
expect(row.getCell(2).value).toEqual(2);
expect(row.getCell(3).value).toEqual(3);

// 通过稀疏数组分配行值(其中数组元素 0 为 `undefined`)
const values = []
values[5] = 7;
values[10] = 'Hello, World!';
row.values = values;
expect(row.getCell(1).value).toBeNull();
expect(row.getCell(5).value).toEqual(7);
expect(row.getCell(10).value).toEqual('Hello, World!');

// 使用列键按对象分配行值
row.values = {
  id: 13,
  name: 'Thing 1',
  dob: new Date()
};

// 在该行下方插入一个分页符
row.addPageBreak();

// 遍历工作表中具有值的所有行
worksheet.eachRow(function(row, rowNumber) {
  console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
});

// 遍历工作表中的所有行(包括空行)
worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
  console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
});

// 连续遍历所有非空单元格
row.eachCell(function(cell, colNumber) {
  console.log('Cell ' + colNumber + ' = ' + cell.value);
});

// 遍历一行中的所有单元格(包括空单元格)
row.eachCell({ includeEmpty: true }, function(cell, colNumber) {
  console.log('Cell ' + colNumber + ' = ' + cell.value);
});

// 提交给流一个完成的行
row.commit();

// 行尺寸
const rowSize = row.cellCount;
const numValues = row.actualCellCount;

Add Rows⬆

// Add a couple of Rows by key-value, after the last current row, using the column keys
worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970,1,1)});
worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965,1,7)});

// Add a row by contiguous Array (assign to columns A, B & C)
worksheet.addRow([3, 'Sam', new Date()]);

// Add a row by sparse Array (assign to columns A, E & I)
const rowValues = [];
rowValues[1] = 4;
rowValues[5] = 'Kyle';
rowValues[9] = new Date();
worksheet.addRow(rowValues);

// Add a row with inherited style
// This new row will have same style as last row
// And return as row object
const newRow = worksheet.addRow(rowValues, 'i');

// Add an array of rows
const rows = [
  [5,'Bob',new Date()], // row by array
  {id:6, name: 'Barbara', dob: new Date()}
];
// add new rows and return them as array of row objects
const newRows = worksheet.addRows(rows);

// Add an array of rows with inherited style
// These new rows will have same styles as last row
// and return them as array of row objects
const newRowsStyled = worksheet.addRows(rows, 'i');
Parameter Description Default Value
value/s The new row/s values
style ‘i’ for inherit from row above, ‘i+’ to include empty cells, ‘n’ for none ‘n’

处理单个单元格⬆

const cell = worksheet.getCell('C3');

// 修改/添加单个单元格
cell.value = new Date(1968, 5, 1);

// 查询单元格的类型
expect(cell.type).toEqual(Excel.ValueType.Date);

// 使用单元格的字符串值
myInput.value = cell.text;

// 使用 html 安全的字符串进行渲染...
const html = '
' + cell.html + '
'
;

合并单元格⬆

// 合并一系列单元格
worksheet.mergeCells('A4:B5');

// ...合并的单元格被链接起来了
worksheet.getCell('B5').value = 'Hello, World!';
expect(worksheet.getCell('B5').value).toBe(worksheet.getCell('A4').value);
expect(worksheet.getCell('B5').master).toBe(worksheet.getCell('A4'));

// ...合并的单元格共享相同的样式对象
expect(worksheet.getCell('B5').style).toBe(worksheet.getCell('A4').style);
worksheet.getCell('B5').style.font = myFonts.arial;
expect(worksheet.getCell('A4').style.font).toBe(myFonts.arial);

// 取消单元格合并将打破链接的样式
worksheet.unMergeCells('A4');
expect(worksheet.getCell('B5').style).not.toBe(worksheet.getCell('A4').style);
expect(worksheet.getCell('B5').style.font).not.toBe(myFonts.arial);

// 按左上,右下合并
worksheet.mergeCells('K10', 'M12');

// 按开始行,开始列,结束行,结束列合并(相当于 K10:M12)
worksheet.mergeCells(10,11,12,13);

Insert Rows⬆

insertRow(pos, value, style = 'n')
insertRows(pos, values, style = 'n')

// Insert a couple of Rows by key-value, shifting down rows every time
worksheet.insertRow(1, {id: 1, name: 'John Doe', dob: new Date(1970,1,1)});
worksheet.insertRow(1, {id: 2, name: 'Jane Doe', dob: new Date(1965,1,7)});

// Insert a row by contiguous Array (assign to columns A, B & C)
worksheet.insertRow(1, [3, 'Sam', new Date()]);

// Insert a row by sparse Array (assign to columns A, E & I)
var rowValues = [];
rowValues[1] = 4;
rowValues[5] = 'Kyle';
rowValues[9] = new Date();
// insert new row and return as row object
const insertedRow = worksheet.insertRow(1, rowValues);

// Insert a row, with inherited style
// This new row will have same style as row on top of it
// And return as row object
const insertedRowInherited = worksheet.insertRow(1, rowValues, 'i');

// Insert a row, keeping original style
// This new row will have same style as it was previously
// And return as row object
const insertedRowOriginal = worksheet.insertRow(1, rowValues, 'o');

// Insert an array of rows, in position 1, shifting down current position 1 and later rows by 2 rows
var rows = [
  [5,'Bob',new Date()], // row by array
  {id:6, name: 'Barbara', dob: new Date()}
];
// insert new rows and return them as array of row objects
const insertedRows = worksheet.insertRows(1, rows);

// Insert an array of rows, with inherited style
// These new rows will have same style as row on top of it
// And return them as array of row objects
const insertedRowsInherited = worksheet.insertRows(1, rows, 'i');

// Insert an array of rows, keeping original style
// These new rows will have same style as it was previously in 'pos' position
const insertedRowsOriginal = worksheet.insertRows(1, rows, 'o');

Parameter Description Default Value
pos Row number where you want to insert, pushing down all rows from there
value/s The new row/s values
style ‘i’ for inherit from row above, , ‘i+’ to include empty cells, ‘o’ for original style, ‘o+’ to include empty cells, ‘n’ for none ‘n’

Splice⬆

// Cut one or more rows (rows below are shifted up)
// Known Issue: If a splice causes any merged cells to move, the results may be unpredictable
worksheet.spliceRows(4, 3);

// remove one row and insert two more.
// Note: rows 4 and below will be shifted down by 1 row.
const newRow3Values = [1, 2, 3, 4, 5];
const newRow4Values = ['one', 'two', 'three', 'four', 'five'];
worksheet.spliceRows(3, 1, newRow3Values, newRow4Values);

// Cut one or more cells (cells to the right are shifted left)
// Note: this operation will not affect other rows
row.splice(3, 2);

// remove one cell and insert two more (cells to the right of the cut cell will be shifted right)
row.splice(4, 1, 'new value 1', 'new value 2');
Parameter Description Default Value
start Starting point to splice from
count Number of rows/cells to remove
…inserts New row/cell values to insert

重复行⬆

duplicateRow(start, amount = 1, insert = true)

const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('duplicateTest');
ws.getCell('A1').value = 'One';
ws.getCell('A2').value = 'Two';
ws.getCell('A3').value = 'Three';
ws.getCell('A4').value = 'Four';

// 该行将重复复制第一行两次,但将替换第二行和第三行
// 如果第三个参数为 true,则它将插入2个新行,其中包含行 “One” 的值和样式
ws.duplicateRow(1,2,false);
参数 描述 默认值
start 要复制的行号(Excel中的第一个是1)
amount 您要复制行的次数 1
insert 如果要为重复项插入新行,则为 true,否则为 false 将替换已有行 true

定义名称⬆

单个单元格(或多个单元格组)可以为它们分配名称。名称可用于公式和数据验证(可能还有更多)。

// 为单元格分配(或获取)名称(将覆盖该单元具有的其他任何名称)
worksheet.getCell('A1').name = 'PI';
expect(worksheet.getCell('A1').name).to.equal('PI');

// 为单元格分配(或获取)一组名称(单元可以具有多个名称)
worksheet.getCell('A1').names = ['thing1', 'thing2'];
expect(worksheet.getCell('A1').names).to.have.members(['thing1', 'thing2']);

// 从单元格中删除名称
worksheet.getCell('A1').removeName('thing1');
expect(worksheet.getCell('A1').names).to.have.members(['thing2']);

数据验证⬆

单元格可以定义哪些值有效或无效,并提示用户以帮助指导它们。

验证类型可以是以下之一:

类型 描述
list 定义一组离散的有效值。Excel 将在下拉菜单中提供这些内容,以便于输入
whole 该值必须是整数
decimal 该值必须是十进制数
textLength 该值可以是文本,但长度是受控的
custom 自定义公式控制有效值

对于 listcustom 以外的其他类型,以下运算符会影响验证:

运算符 描述
between 值必须介于公式结果之间
notBetween 值不能介于公式结果之间
equal 值必须等于公式结果
notEqual 值不能等于公式结果
greaterThan 值必须大于公式结果
lessThan 值必须小于公式结果
greaterThanOrEqual 值必须大于或等于公式结果
lessThanOrEqual 值必须小于或等于公式结果
// 指定有效值的列表(One,Two,Three,Four)。
// Excel 将提供一个包含这些值的下拉列表。
worksheet.getCell('A1').dataValidation = {
  type: 'list',
  allowBlank: true,
  formulae: ['"One,Two,Three,Four"']
};

// 指定范围内的有效值列表。
// Excel 将提供一个包含这些值的下拉列表。
worksheet.getCell('A1').dataValidation = {
  type: 'list',
  allowBlank: true,
  formulae: ['$D$5:$F$5']
};

// 指定单元格必须为非5的整数。
// 向用户显示适当的错误消息(如果他们弄错了)
worksheet.getCell('A1').dataValidation = {
  type: 'whole',
  operator: 'notEqual',
  showErrorMessage: true,
  formulae: [5],
  errorStyle: 'error',
  errorTitle: 'Five',
  error: 'The value must not be Five'
};

// 指定单元格必须为1.5到7之间的十进制数字。
// 添加“工具提示”以帮助指导用户
worksheet.getCell('A1').dataValidation = {
  type: 'decimal',
  operator: 'between',
  allowBlank: true,
  showInputMessage: true,
  formulae: [1.5, 7],
  promptTitle: 'Decimal',
  prompt: 'The value must between 1.5 and 7'
};

// 指定单元格的文本长度必须小于15
worksheet.getCell('A1').dataValidation = {
  type: 'textLength',
  operator: 'lessThan',
  showErrorMessage: true,
  allowBlank: true,
  formulae: [15]
};

// 指定单元格必须是2016年1月1日之前的日期
worksheet.getCell('A1').dataValidation = {
  type: 'date',
  operator: 'lessThan',
  showErrorMessage: true,
  allowBlank: true,
  formulae: [new Date(2016,0,1)]
};

单元格注释⬆

将旧样式的注释添加到单元格

// 纯文字笔记
worksheet.getCell('A1').note = 'Hello, ExcelJS!';

// 彩色格式化的笔记
ws.getCell('B1').note = {
  texts: [
    {'font': {'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'This is '},
    {'font': {'italic': true, 'size': 12, 'color': {'theme': 0}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'a'},
    {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' '},
    {'font': {'size': 12, 'color': {'argb': 'FFFF6600'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'colorful'},
    {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' text '},
    {'font': {'size': 12, 'color': {'argb': 'FFCCFFCC'}, 'name': 'Calibri', 'scheme': 'minor'}, 'text': 'with'},
    {'font': {'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': ' in-cell '},
    {'font': {'bold': true, 'size': 12, 'color': {'theme': 1}, 'name': 'Calibri', 'family': 2, 'scheme': 'minor'}, 'text': 'format'},
  ],
  margins: {
    insetmode: 'custom',
    inset: [0.25, 0.25, 0.35, 0.35]
  },
  protection: {
    locked: True,
    lockText: False
  },
  editAs: 'twoCells'
};

单元格批注属性⬆

下表定义了单元格注释已支持的属性。

Field Required Default Value Description
texts Y 评论文字
margins N {} 确定自动或自定义设置单元格注释的边距值
protection N {} 可以使用保护属性来指定对象和对象文本的锁定状态
editAs N ‘absolute’ 可以使用’editAs’属性来指定注释如何锚定到单元格

单元格批注页边距⬆

确定单元格批注的页面距设置模式,自动或者自定义模式。

ws.getCell('B1').note.margins = {
  insetmode: 'custom',
  inset: [0.25, 0.25, 0.35, 0.35]
}

已支持的页边距属性⬆

Property Required Default Value Description
insetmode N ‘auto’ 确定是否自动设置注释边距,并且值是’auto’ 或者 ‘custom’
inset N [0.13, 0.13, 0.25, 0.25] 批注页边距的值,单位是厘米, 方向是左-上-右-下

注意:只有当 insetmode的值设置为’custom’时,inset的设置才生效。

单元格批注保护⬆

可以使用保护属性来修改单元级别保护。

ws.getCell('B1').note.protection = {
  locked: 'False',
  lockText: 'False',
};

已支持的保护属性⬆

Property Required Default Value Description
locked N ‘True’ 此元素指定在保护工作表时对象已锁定
lockText N ‘True’ 该元素指定对象的文本已锁定

单元格批注对象位置属性⬆

单元格注释还可以具有属性 ‘editAs’,该属性将控制注释如何锚定到单元格。
它可以具有以下值之一:

ws.getCell('B1').note.editAs = 'twoCells'
Value Description
twoCells 它指定注释的大小、位置随单元格而变
oneCells 它指定注释的大小固定,位置随单元格而变
absolute 这是默认值,它指定注释的大小、位置均固定

表格⬆

表允许表格内数据的表内操作。

要将表添加到工作表,请定义表模型并调用 addTable

// 将表格添加到工作表
ws.addTable({
  name: 'MyTable',
  ref: 'A1',
  headerRow: true,
  totalsRow: true,
  style: {
    theme: 'TableStyleDark3',
    showRowStripes: true,
  },
  columns: [
    {name: 'Date', totalsRowLabel: 'Totals:', filterButton: true},
    {name: 'Amount', totalsRowFunction: 'sum', filterButton: false},
  ],
  rows: [
    [new Date('2019-07-20'), 70.10],
    [new Date('2019-07-21'), 70.60],
    [new Date('2019-07-22'), 70.10],
  ],
});

注意:将表格添加到工作表将通过放置表格的标题和行数据来修改工作表。
结果就是表格覆盖的工作表上的所有数据(包括标题和所有的)都将被覆盖。

表格属性⬆

下表定义了表格支持的属性。

表属性 描述 是否需要 默认值
name 表格名称 Y
displayName 表格的显示名称 N name
ref 表格的左上方单元格 Y
headerRow 在表格顶部显示标题 N true
totalsRow 在表格底部显示总计 N false
style 额外的样式属性 N {}
columns 列定义 Y
rows 数据行 Y

表格样式属性⬆

下表定义了表格中支持的属性样式属性。

样式属性 描述 是否需要 默认值
theme 桌子的颜色主题 N 'TableStyleMedium2'
showFirstColumn 突出显示第一列(粗体) N false
showLastColumn 突出显示最后一列(粗体) N false
showRowStripes 用交替的背景色显示行 N false
showColumnStripes 用交替的背景色显示列 N false

表格列属性⬆

下表定义了每个表格列中支持的属性。

列属性 描述 是否需要 默认值
name 列名,也用在标题中 Y
filterButton 切换标题中的过滤器控件 N false
totalsRowLabel 用于描述统计行的标签(第一列) N 'Total'
totalsRowFunction 统计函数名称 N 'none'
totalsRowFormula 自定义函数的可选公式 N

统计函数⬆

下表列出了由列定义的 totalsRowFunction 属性的有效值。如果使用 'custom' 以外的任何值,则无需包括关联的公式,因为该公式将被表格插入。

统计函数 描述
none 此列没有统计函数
average 计算列的平均值
countNums 统计数字条目数
count 条目数
max 此列中的最大值
min 此列中的最小值
stdDev 该列的标准偏差
var 此列的方差
sum 此列的条目总数
custom 自定义公式。 需要关联的 totalsRowFormula 值。

表格样式主题⬆

有效的主题名称遵循以下模式:

  • “TableStyle[Shade][Number]”

Shades(阴影),Number(数字)可以是以下之一:

  • Light, 1-21
  • Medium, 1-28
  • Dark, 1-11

对于无主题,请使用值 null

注意:exceljs 尚不支持自定义表格主题。

修改表格⬆

表格支持一组操作函数,这些操作函数允许添加或删除数据以及更改某些属性。由于这些操作中的许多操作可能会对工作表产生副作用,因此更改必须在完成后立即提交。

表中的所有索引值均基于零,因此第一行号和第一列号为 0

添加或删除标题和统计

const table = ws.getTable('MyTable');

// 打开标题行
table.headerRow = true;

// 关闭统计行
table.totalsRow = false;

// 将表更改提交到工作表中
table.commit();

重定位表

const table = ws.getTable('MyTable');

// 表格左上移至 D4
table.ref = 'D4';

// 将表更改提交到工作表中
table.commit();

添加和删除行

const table = ws.getTable('MyTable');

// 删除前两行
table.removeRows(0, 2);

// 在索引 5 处插入新行
table.addRow([new Date('2019-08-05'), 5, 'Mid'], 5);

// 在表格底部追加新行
table.addRow([new Date('2019-08-10'), 10, 'End']);

// 将表更改提交到工作表中
table.commit();

添加和删除列

const table = ws.getTable('MyTable');

// 删除第二列
table.removeColumns(1, 1);

// 在索引 1 处插入新列(包含数据)
table.addColumn(
  {name: 'Letter', totalsRowFunction: 'custom', totalsRowFormula: 'ROW()', totalsRowResult: 6, filterButton: true},
  ['a', 'b', 'c', 'd'],
  2
);

// 将表更改提交到工作表中
table.commit();

更改列属性

const table = ws.getTable('MyTable');

// 获取第二列的列包装器
const column = table.getColumn(1);

// 设置一些属性
column.name = 'Code';
column.filterButton = true;
column.style = {font:{bold: true, name: 'Comic Sans MS'}};
column.totalsRowLabel = 'Totals';
column.totalsRowFunction = 'custom';
column.totalsRowFormula = 'ROW()';
column.totalsRowResult = 10;

// 将表更改提交到工作表中
table.commit();

样式⬆

单元格,行和列均支持一组丰富的样式和格式,这些样式和格式会影响单元格的显示方式。

通过分配以下属性来设置样式:

  • numFmt
  • font
  • alignment
  • border
  • fill
// 为单元格分配样式
ws.getCell('A1').numFmt = '0.00%';

// 将样式应用于工作表列
ws.columns = [
  { header: 'Id', key: 'id', width: 10 },
  { header: 'Name', key: 'name', width: 32, style: { font: { name: 'Arial Black' } } },
  { header: 'D.O.B.', key: 'DOB', width: 10, style: { numFmt: 'dd/mm/yyyy' } }
];

// 将第3列设置为“货币格式”
ws.getColumn(3).numFmt = '"£"#,##0.00;[Red]\-"£"#,##0.00';

// 将第2行设置为 Comic Sans。
ws.getRow(2).font = { name: 'Comic Sans MS', family: 4, size: 16, underline: 'double', bold: true };

将样式应用于行或列时,它将应用于该行或列中所有当前存在的单元格。另外,创建的任何新单元格都将从其所属的行和列继承其初始样式。

如果单元格的行和列都定义了特定的样式(例如,字体),则该单元格所在行样式比列样式具有更高优先级。但是,如果行和列定义了不同的样式(例如 column.numFmtrow.font),则单元格将继承行的字体和列的 numFmt

注意:以上所有属性(numFmt(字符串)除外)都是 JS 对象结构。如果将同一样式对象分配给多个电子表格实体,则每个实体将共享同一样式对象。如果样式对象后来在电子表格序列化之前被修改,则所有引用该样式对象的实体也将被修改。此行为旨在通过减少创建的JS对象的数量来优先考虑性能。如果希望样式对象是独立的,则需要先对其进行克隆,然后再分配它们。同样,默认情况下,如果电子表格实体共享相似的样式,则从文件(或流)中读取文档时,它们也将引用相同的样式对象。

数字格式⬆

// 将值显示为“ 1 3/5”
ws.getCell('A1').value = 1.6;
ws.getCell('A1').numFmt = '# ?/?';

// 显示为“ 1.60%”
ws.getCell('B1').value = 0.016;
ws.getCell('B1').numFmt = '0.00%';

字体


// for the wannabe graphic designers out there
ws.getCell('A1').font = {
  name: 'Comic Sans MS',
  family: 4,
  size: 16,
  underline: true,
  bold: true
};

// for the graduate graphic designers...
ws.getCell('A2').font = {
  name: 'Arial Black',
  color: { argb: 'FF00FF00' },
  family: 2,
  size: 14,
  italic: true
};

// 垂直对齐
ws.getCell('A3').font = {
  vertAlign: 'superscript'
};

// 注意:该单元格将存储对分配的字体对象的引用。
// 如果之后更改了字体对象,则单元字体也将更改。
const font = { name: 'Arial', size: 12 };
ws.getCell('A3').font = font;
font.size = 20; // 单元格 A3 现在具有20号字体!

// 从文件或流中读取工作簿后,共享相似字体的单元格可能引用相同的字体对象
字体属性 描述 示例值
name 字体名称。 ‘Arial’, ‘Calibri’, etc.
family 备用字体家族。整数值。 1 - Serif, 2 - Sans Serif, 3 - Mono, Others - unknown
scheme 字体方案。 ‘minor’, ‘major’, ‘none’
charset 字体字符集。整数值。 1, 2, etc.
size 字体大小。整数值。 9, 10, 12, 16, etc.
color 颜色描述,一个包含 ARGB 值的对象。 { argb: ‘FFFF0000’}
bold 字体 粗细 true, false
italic 字体 倾斜 true, false
underline 字体 下划线 样式 true, false, ‘none’, ‘single’, ‘double’, ‘singleAccounting’, ‘doubleAccounting’
strike 字体 删除线 true, false
outline 字体轮廓 true, false
vertAlign 垂直对齐 ‘superscript’, ‘subscript’

对齐⬆

// 将单元格对齐方式设置为左上,中间居中,右下
ws.getCell('A1').alignment = { vertical: 'top', horizontal: 'left' };
ws.getCell('B1').alignment = { vertical: 'middle', horizontal: 'center' };
ws.getCell('C1').alignment = { vertical: 'bottom', horizontal: 'right' };

// 将单元格设置为自动换行
ws.getCell('D1').alignment = { wrapText: true };

// 将单元格缩进设置为1
ws.getCell('E1').alignment = { indent: 1 };

// 将单元格文本旋转设置为向上30deg,向下45deg和垂直文本
ws.getCell('F1').alignment = { textRotation: 30 };
ws.getCell('G1').alignment = { textRotation: -45 };
ws.getCell('H1').alignment = { textRotation: 'vertical' };

有效的对齐属性值

水平的 垂直 文本换行 自适应 缩进 阅读顺序 文本旋转
left top true true integer rtl 0 to 90
center middle false false ltr -1 to -90
right bottom vertical
fill distributed
justify justify
centerContinuous
distributed

边框⬆

// 在A1周围设置单个细边框
ws.getCell('A1').border = {
  top: {style:'thin'},
  left: {style:'thin'},
  bottom: {style:'thin'},
  right: {style:'thin'}
};

// 在A3周围设置双细绿色边框
ws.getCell('A3').border = {
  top: {style:'double', color: {argb:'FF00FF00'}},
  left: {style:'double', color: {argb:'FF00FF00'}},
  bottom: {style:'double', color: {argb:'FF00FF00'}},
  right: {style:'double', color: {argb:'FF00FF00'}}
};

// 在A5中设置厚红十字边框
ws.getCell('A5').border = {
  diagonal: {up: true, down: true, style:'thick', color: {argb:'FFFF0000'}}
};

有效边框样式

  • thin
  • dotted
  • dashDot
  • hair
  • dashDotDot
  • slantDashDot
  • mediumDashed
  • mediumDashDotDot
  • mediumDashDot
  • medium
  • double
  • thick

填充⬆

// 用红色深色垂直条纹填充A1
ws.getCell('A1').fill = {
  type: 'pattern',
  pattern:'darkVertical',
  fgColor:{argb:'FFFF0000'}
};

// 在A2中填充深黄色格子和蓝色背景
ws.getCell('A2').fill = {
  type: 'pattern',
  pattern:'darkTrellis',
  fgColor:{argb:'FFFFFF00'},
  bgColor:{argb:'FF0000FF'}
};

// 从左到右用蓝白蓝渐变填充A3
ws.getCell('A3').fill = {
  type: 'gradient',
  gradient: 'angle',
  degree: 0,
  stops: [
    {position:0, color:{argb:'FF0000FF'}},
    {position:0.5, color:{argb:'FFFFFFFF'}},
    {position:1, color:{argb:'FF0000FF'}}
  ]
};


// 从中心开始用红绿色渐变填充A4
ws.getCell('A4').fill = {
  type: 'gradient',
  gradient: 'path',
  center:{left:0.5,top:0.5},
  stops: [
    {position:0, color:{argb:'FFFF0000'}},
    {position:1, color:{argb:'FF00FF00'}}
  ]
};

填充模式⬆

属性 是否需要 描述
type Y 值: 'pattern'
指定此填充使用模式
pattern Y 指定模式类型 (查看下面 有效模式类型 )
fgColor N 指定图案前景色。默认为黑色。
bgColor N 指定图案背景色。默认为白色。

有效模式类型

  • none
  • solid
  • darkGray
  • mediumGray
  • lightGray
  • gray125
  • gray0625
  • darkHorizontal
  • darkVertical
  • darkDown
  • darkUp
  • darkGrid
  • darkTrellis
  • lightHorizontal
  • lightVertical
  • lightDown
  • lightUp
  • lightGrid
  • lightTrellis

渐变填充⬆

属性 是否需要 描述
type Y 值: 'gradient'
指定此填充使用渐变
gradient Y 指定渐变类型。['angle','path'] 之一
degree angle 对于“角度”渐变,指定渐变的方向。0 是从左到右。值从 1-359 顺时针旋转方向
center path 对于“路径”渐变。指定路径起点的相对坐标。“左”和“顶”值的范围是 0 到 1
stops Y 指定渐变颜色序列。是包含位置和颜色(从位置 0 开始到位置 1 结束)的对象的数组。中间位置可用于指定路径上的其他颜色。

注意事项

使用上面的接口,可能会创建使用XLSX编辑器程序无法实现的渐变填充效果。例如,Excel 仅支持0、45、90 和 135 的角度梯度。类似地,stops 的顺序也可能受到 UI 的限制,其中位置 [0,1] 或[0,0.5,1] 是唯一的选择。请谨慎处理此填充,以确保目标 XLSX 查看器支持该填充。

富文本⬆

现在,单个单元格支持RTF文本或单元格格式化。富文本值可以控制文本值内任意数量的子字符串的字体属性。有关支持哪些字体属性的详细信息,请参见字体。


ws.getCell('A1').value = {
  'richText': [
    {'font': {'size': 12,'color': {'theme': 0},'name': 'Calibri','family': 2,'scheme': 'minor'},'text': 'This is '},
    {'font': {'italic': true,'size': 12,'color': {'theme': 0},'name': 'Calibri','scheme': 'minor'},'text': 'a'},
    {'font': {'size': 12,'color': {'theme': 1},'name': 'Calibri','family': 2,'scheme': 'minor'},'text': ' '},
    {'font': {'size': 12,'color': {'argb': 'FFFF6600'},'name': 'Calibri','scheme': 'minor'},'text': 'colorful'},
    {'font': {'size': 12,'color': {'theme': 1},'name': 'Calibri','family': 2,'scheme': 'minor'},'text': ' text '},
    {'font': {'size': 12,'color': {'argb': 'FFCCFFCC'},'name': 'Calibri','scheme': 'minor'},'text': 'with'},
    {'font': {'size': 12,'color': {'theme': 1},'name': 'Calibri','family': 2,'scheme': 'minor'},'text': ' in-cell '},
    {'font': {'bold': true,'size': 12,'color': {'theme': 1},'name': 'Calibri','family': 2,'scheme': 'minor'},'text': 'format'}
  ]
};

expect(ws.getCell('A1').text).to.equal('This is a colorful text with in-cell format');
expect(ws.getCell('A1').type).to.equal(Excel.ValueType.RichText);

单元格保护⬆

可以使用保护属性来修改单元级别保护。

ws.getCell('A1').protection = {
  locked: false,
  hidden: true,
};

支持的保护属性

属性 默认值 描述
locked true 指定在工作表受保护的情况下是否将单元格锁定。
hidden false 指定如果工作表受保护,则单元格的公式是否可见。

条件格式化⬆

条件格式化允许工作表根据单元格值或任意公式显示特定的样式,图标等。

条件格式设置规则是在工作表级别添加的,通常会覆盖一系列单元格。

可以将多个规则应用于给定的单元格范围,并且每个规则都将应用自己的样式。

如果多个规则影响给定的单元格,则规则优先级值将确定如果竞争样式冲突,则哪个规则胜出。优先级值较低的规则获胜。如果没有为给定规则指定优先级值,ExcelJS 将按升序分配它们。

注意:目前,仅支持条件格式设置规则的子集。具体来说,只有格式规则不需要

// 根据行和列为偶数或奇数向 A1:E7 添加一个棋盘图案
worksheet.addConditionalFormatting({
  ref: 'A1:E7',
  rules: [
    {
      type: 'expression',
      formulae: ['MOD(ROW()+COLUMN(),2)=0'],
      style: {fill: {type: 'pattern', pattern: 'solid', bgColor: {argb: 'FF00FF00'}}},
    }
  ]
})

支持的条件格式设置规则类型

类型 描述
expression 任何自定义功能均可用于激活规则。
cellIs 使用指定的运算符将单元格值与提供的公式进行比较
top10 将格式化应用于值在顶部(或底部)范围内的单元格
aboveAverage 将格式化应用于值高于(或低于)平均值的单元格
colorScale 根据其值在范围内的位置将彩色背景应用于单元格
iconSet 根据值将一系列图标之一添加到单元格
containsText 根据单元格是否为特定文本来应用格式
timePeriod 根据单元格日期时间值是否在指定范围内应用格式

表达式⬆

属性 可选 默认值 描述
type 'expression'
priority Y 确定样式的优先顺序
formulae 1个包含真/假值的公式字符串数组。要引用单元格值,请使用左上角的单元格地址
style 公式返回 true 时要应用的样式结构

Cell Is⬆

属性 可选 默认值 描述
type 'cellIs'
priority Y 确定样式的优先顺序
operator 如何将单元格值与公式结果进行比较
formulae 1个公式字符串数组,返回要与每个单元格进行比较的值
style 如果比较返回 true,则应用样式结构

Cell Is 运算符

运算 描述
equal 如果单元格值等于公式值,则应用格式
greaterThan 如果单元格值大于公式值,则应用格式
lessThan 如果单元格值小于公式值,则应用格式
between 如果单元格值在两个公式值之间(包括两个值),则应用格式

Top 10⬆

属性 可选 默认值 描述
type 'top10'
priority Y 确定样式的优先顺序
rank Y 10 指定格式中包含多少个顶部(或底部)值
percent Y false 如果为 true,则等级字段为百分比,而不是绝对值
bottom Y false 如果为 true,则包含最低值而不是最高值
style 如果比较返回 true,则应用样式结构

高于平均值⬆

属性 可选 默认值 描述
type 'aboveAverage'
priority Y 确定样式的优先顺序
aboveAverage Y false 如果为 true,则等级字段为百分比,而不是绝对值
style 如果比较返回 true,则应用样式结构

色阶⬆

属性 可选 默认值 描述
type 'colorScale'
priority Y 确定样式的优先顺序
cfvo 2到5个条件格式化值对象的数组,指定值范围内的航路点
color 在给定的航路点使用的相应颜色数组
style 如果比较返回 true,则应用样式结构

图标集⬆

属性 可选 默认值 描述
type 'iconSet'
priority Y 确定样式的优先顺序
iconSet Y 3TrafficLights 设置使用的图标名称
showValue true 指定应用范围内的单元格是显示图标和单元格值,还是仅显示图标
reverse false 指定是否以保留顺序显示 iconSet 中指定的图标集中的图标。 如果 custom 等于 true,则必须忽略此值
custom false 指定是否使用自定义图标集
cfvo 2到5个条件格式化值对象的数组,指定值范围内的航路点
style 如果比较返回 true,则应用样式结构

数据条⬆

字段 可选 默认值 描述
type 'dataBar'
priority Y 确定样式的优先顺序
minLength 0 指定此条件格式范围内最短数据条的长度
maxLength 100 指定此条件格式范围内最长数据条的长度
showValue true 指定条件格式范围内的单元格是否同时显示数据条和数值或数据条
gradient true 指定数据条是否具有渐变填充
border true 指定数据条是否有边框
negativeBarColorSameAsPositive true 指定数据条是否具有与正条颜色不同的负条颜色
negativeBarBorderColorSameAsPositive true 指定数据条的负边框颜色是否不同于正边框颜色
axisPosition ‘auto’ 指定数据条的轴位置
direction ‘leftToRight’ 指定数据条的方向
cfvo 2 到 5 个条件格式化值对象的数组,指定值范围内的航路点
style 如果比较返回 true,则应用样式结构

包含文字⬆

属性 可选 默认值 描述
type 'containsText'
priority Y 确定样式的优先顺序
operator 文本比较类型
text 要搜索的文本
style 如果比较返回 true,则应用样式结构

包含文本运算符

运算符 描述
containsText 如果单元格值包含在 text 字段中指定的值,则应用格式
containsBlanks 如果单元格值包含空格,则应用格式
notContainsBlanks 如果单元格值不包含空格,则应用格式
containsErrors 如果单元格值包含错误,则应用格式
notContainsErrors 如果单元格值不包含错误,则应用格式

时间段⬆

属性 可选 默认值 描述
type 'timePeriod'
priority Y 确定样式的优先顺序
timePeriod 比较单元格值的时间段
style 如果比较返回 true,则应用样式结构

时间段

时间段 描述
lastWeek 如果单元格值落在最后一周内,则应用格式
thisWeek 如果单元格值在本周下降,则应用格式
nextWeek 如果单元格值在下一周下降,则应用格式
yesterday 如果单元格值等于昨天,则应用格式
today 如果单元格值等于今天,则应用格式
tomorrow 如果单元格值等于明天,则应用格式
last7Days 如果单元格值在过去7天之内,则应用格式
lastMonth 如果单元格值属于上个月,则应用格式
thisMonth 如果单元格值在本月下降,则应用格式
nextMonth 如果单元格值在下个月下降,则应用格式

大纲级别⬆

Excel 支持大纲;行或列可以根据用户希望查看的详细程度展开或折叠。

大纲级别可以在列设置中定义:

worksheet.columns = [
  { header: 'Id', key: 'id', width: 10 },
  { header: 'Name', key: 'name', width: 32 },
  { header: 'D.O.B.', key: 'DOB', width: 10, outlineLevel: 1 }
];

或直接在行或列上

worksheet.getColumn(3).outlineLevel = 1;
worksheet.getRow(3).outlineLevel = 1;

工作表大纲级别可以在工作表上设置

// 设置列大纲级别
worksheet.properties.outlineLevelCol = 1;

// 设置行大纲级别
worksheet.properties.outlineLevelRow = 1;

注意:调整行或列上的大纲级别或工作表上的大纲级别将产生副作用,即还修改受属性更改影响的所有行或列的折叠属性。 例如。:

worksheet.properties.outlineLevelCol = 1;

worksheet.getColumn(3).outlineLevel = 1;
expect(worksheet.getColumn(3).collapsed).to.be.true;

worksheet.properties.outlineLevelCol = 2;
expect(worksheet.getColumn(3).collapsed).to.be.false;

大纲属性可以在工作表上设置

worksheet.properties.outlineProperties = {
  summaryBelow: false,
  summaryRight: false,
};

图片⬆

将图像添加到工作表是一个分为两个步骤的过程。首先,通过 addImage() 函数将图像添加到工作簿中,该函数还将返回 imageId 值。然后,使用 imageId,可以将图像作为平铺背景或覆盖单元格区域添加到工作表中。

注意:从此版本开始,不支持调整或变换图像。

将图片添加到工作簿⬆

Workbook.addImage 函数支持按文件名或按 Buffer 添加图像。请注意,在两种情况下,都必须指定扩展名。有效的扩展名包括 “jpeg”,“png”,“gif”。

// 通过文件名将图像添加到工作簿
const imageId1 = workbook.addImage({
  filename: 'path/to/image.jpg',
  extension: 'jpeg',
});

// 通过 buffer 将图像添加到工作簿
const imageId2 = workbook.addImage({
  buffer: fs.readFileSync('path/to.image.png'),
  extension: 'png',
});

// 通过 base64  将图像添加到工作簿
const myBase64Image = "data:image/png;base64,iVBORw0KG...";
const imageId2 = workbook.addImage({
  base64: myBase64Image,
  extension: 'png',
});

将图片添加到工作表背景⬆

使用 Workbook.addImage 中的图像 ID,可以使用 addBackgroundImage 函数设置工作表的背景

// 设置背景
worksheet.addBackgroundImage(imageId1);

在一定范围内添加图片⬆

使用 Workbook.addImage 中的图像 ID,可以将图像嵌入工作表中以覆盖一定范围。从该范围计算出的坐标将覆盖从第一个单元格的左上角到第二个单元格的右下角。

// 在 B2:D6 上插入图片
worksheet.addImage(imageId2, 'B2:D6');

使用结构而不是范围字符串,可以部分覆盖单元格。

请注意,为此使用的坐标系基于零,因此 A1 的左上角将为 {col:0,row:0}。单元格的分数可以通过使用浮点数来指定,例如 A1 的中点是 {col:0.5,row:0.5}

// 在 B2:D6 的一部分上插入图像
worksheet.addImage(imageId2, {
  tl: { col: 1.5, row: 1.5 },
  br: { col: 3.5, row: 5.5 }
});

单元格区域还可以具有属性 "editAs",该属性将控制将图像锚定到单元格的方式。它可以具有以下值之一:

描述
undefined 它指定使图像将根据单元格移动和调整其大小
oneCell 这是默认值。图像将与单元格一起移动,但大小不变动
absolute 图像将不会随着单元格移动或调整大小
ws.addImage(imageId, {
  tl: { col: 0.1125, row: 0.4 },
  br: { col: 2.101046875, row: 3.4 },
  editAs: 'oneCell'
});

将图片添加到单元格⬆

您可以将图像添加到单元格,然后以 96dpi 定义其宽度和高度(以像素为单位)。

worksheet.addImage(imageId2, {
  tl: { col: 0, row: 0 },
  ext: { width: 500, height: 200 }
});

添加带有超链接的图片⬆

您可以将带有超链接的图像添加到单元格,并在图像范围内定义超链接。

worksheet.addImage(imageId2, {
  tl: { col: 0, row: 0 },
  ext: { width: 500, height: 200 },
  hyperlinks: {
    hyperlink: 'http://www.somewhere.com',
    tooltip: 'http://www.somewhere.com'
  }
});

工作表保护⬆

可以通过添加密码来保护工作表免受修改。

await worksheet.protect('the-password', options);

工作表保护也可以删除:

worksheet.unprotect();

有关如何修改单个单元格保护的详细信息请查看 单元格保护。

注意:protect() 函数返回一个 Promise 代表它是异步的,当前的实现在主线程上运行,并且在 CPU 上将使用平均大约 600 毫秒。可以通过设置 spinCount 进行调整,该值可用于使过程更快或更有弹性。

工作表保护选项⬆

属性 默认值 描述
selectLockedCells true 允许用户选择锁定的单元格
selectUnlockedCells true 允许用户选择未锁定的单元格
formatCells false 允许用户格式化单元格
formatColumns false 允许用户格式化列
formatRows false 允许用户格式化行
insertRows false 允许用户插入行
insertColumns false 允许用户插入列
insertHyperlinks false 允许用户插入超链接
deleteRows false 允许用户删除行
deleteColumns false 允许用户删除列
sort false 允许用户对数据进行排序
autoFilter false 允许用户过滤表中的数据
pivotTables false 允许用户使用数据透视表
spinCount 100000 保护或取消保护时执行的哈希迭代次数

文件 I/O⬆

XLSX⬆

读 XLSX⬆

// 从文件读取
const workbook = new Excel.Workbook();
await workbook.xlsx.readFile(filename);
// ... 使用 workbook


// 从流读取
const workbook = new Excel.Workbook();
await workbook.xlsx.read(stream);
// ... 使用 workbook


// 从 buffer 加载
const workbook = new Excel.Workbook();
await workbook.xlsx.load(data);
// ... 使用 workbook

写 XLSX

// 写入文件
const workbook = createAndFillWorkbook();
await workbook.xlsx.writeFile(filename);

// 写入流
await workbook.xlsx.write(stream);

// 写入 buffer
const buffer = await workbook.xlsx.writeBuffer();

CSV⬆

读 CSV⬆

读取 CSV 文件时支持的选项。

属性 是否需要 类型 描述
dateFormats N Array 指定 dayjs 的日期编码格式。
map N Function 自定义Array.prototype.map() 回调函数,用于处理数据。
sheetName N String 指定工作表名称。
parserOptions N Object parseOptions 选项 @fast-csv/format 模块以写入 csv 数据。
// 从文件读取
const workbook = new Excel.Workbook();
const worksheet = await workbook.csv.readFile(filename);
// ... 使用 workbook 或 worksheet


// 从流中读取
const workbook = new Excel.Workbook();
const worksheet = await workbook.csv.read(stream);
// ... 使用 workbook 或 worksheet


// 从带有欧洲日期的文件中读取
const workbook = new Excel.Workbook();
const options = {
  dateFormats: ['DD/MM/YYYY']
};
const worksheet = await workbook.csv.readFile(filename, options);
// ... 使用 workbook 或 worksheet


// 从具有自定义值解析的文件中读取
const workbook = new Excel.Workbook();
const options = {
  map(value, index) {
    switch(index) {
      case 0:
        // 第1列是字符串
        return value;
      case 1:
        // 第2列是日期
        return new Date(value);
      case 2:
        // 第3列是公式值的JSON
        return JSON.parse(value);
      default:
        // 其余的是数字
        return parseFloat(value);
    }
  },
  // https://c2fo.io/fast-csv/docs/parsing/options
  parserOptions: {
    delimiter: '\t',
    quote: false,
  },
};
const worksheet = await workbook.csv.readFile(filename, options);
// ... 使用 workbook 或 worksheet

CSV 解析器使用 fast-csv 读取CSV文件。传递给上述写入函数的选项中的 formatterOptions 将传递给 @fast-csv/format 模块以写入 csv 数据。 有关详细信息,请参阅 fast-csv README.md。

使用 npm 模块 dayjs 解析日期。如果未提供 dateFormats 数组,则使用以下 dateFormats:

  • ‘YYYY-MM-DD[T]HH:mm:ss’
  • ‘MM-DD-YYYY’
  • ‘YYYY-MM-DD’

请参阅 dayjs CustomParseFormat 插件,以获取有关如何构造 dateFormat 的详细信息。

写 CSV⬆

写入 CSV 文件时支持的选项。

属性 是否需要 类型 描述
dateFormat N String 指定 dayjs 的日期编码格式。
dateUTC N Boolean 指定 ExcelJS 是否使用dayjs.utc()转换时区以解析日期。
encoding N String 指定文件编码格式。
includeEmptyRows N Boolean 指定是否可以写入空行。
map N Function 自定义Array.prototype.map() 回调函数,用于处理行值。
sheetName N String 指定工作表名称。
sheetId N Number 指定工作表 ID。
formatterOptions N Object formatterOptions 选项 @fast-csv/format 模块写入csv 数据。

// 写入文件
const workbook = createAndFillWorkbook();
await workbook.csv.writeFile(filename);

// 写入流
// 请注意,您需要提供 sheetName 或 sheetId 以正确导入到 csv
await workbook.csv.write(stream, { sheetName: 'Page name' });

// 使用欧洲日期时间写入文件
const workbook = new Excel.Workbook();
const options = {
  dateFormat: 'DD/MM/YYYY HH:mm:ss',
  dateUTC: true, // 呈现日期时使用 utc
};
await workbook.csv.writeFile(filename, options);


// 使用自定义值格式写入文件
const workbook = new Excel.Workbook();
const options = {
  map(value, index) {
    switch(index) {
      case 0:
        // 第1列是字符串
        return value;
      case 1:
        // 第2列是日期
        return moment(value).format('YYYY-MM-DD');
      case 2:
        // 第3列是一个公式,只写结果
        return value.result;
      default:
        // 其余的是数字
        return value;
    }
  },
  // https://c2fo.io/fast-csv/docs/formatting/options
  formatterOptions: {
    delimiter: '\t',
    quote: false,
  },
};
await workbook.csv.writeFile(filename, options);

// 写入新 buffer
const buffer = await workbook.csv.writeBuffer();

CSV 解析器使用 fast-csv 编写 CSV 文件。传递给上述写入函数的选项中的 formatterOptions 将传递给 @fast-csv/format 模块以写入 csv 数据。有关详细信息,请参阅 fast-csv README.md。

日期使用 npm 模块 moment 格式化。如果未提供 dateFormat,则使用 moment.ISO_8601。编写 CSV 时,您可以提供布尔值 dateUTCtrue,以使 ExcelJS 解析日期,而无需使用 moment.utc() 自动转换时区。

流式 I/O⬆

上面记录的文件 I/O 需要在内存中建立整个工作簿,然后才能写入文件。虽然方便,但是由于所需的内存量,它可能会限制文档的大小。

流写入器(或读取器)在生成工作簿或工作表数据时对其进行处理,然后将其转换为文件形式。通常,这在内存上效率要高得多,因为最终的内存占用量,甚至中间的内存占用量都比文档版本要紧凑得多,尤其是当您考虑到行和单元格对象一旦提交就被销毁时,尤其如此。

流式工作簿和工作表的接口几乎与文档版本相同,但实际存在一些细微差别:

  • 将工作表添加到工作簿后,将无法将其删除。
  • 提交行后,将无法再访问该行,因为该行将从工作表中删除。
  • 不支持 unMergeCells()

请注意,可以在不提交任何行的情况下构建整个工作簿。提交工作簿后,所有添加的工作表(包括所有未提交的行)将自动提交。但是,在这种情况下,与文档版本相比收效甚微。

流式 XLSX⬆

流式 XLSX 写入器⬆

流式 XLSX 写入器在 ExcelJS.stream.xlsx 命名空间中可用。

构造函数采用带有以下字段的可选 options 对象:

字段 描述
stream 指定要写入 XLSX 工作簿的可写流。
filename 如果未指定流,则此字段指定要写入 XLSX 工作簿的文件的路径。
useSharedStrings 指定是否在工作簿中使用共享字符串。默认为 false
useStyles 指定是否将样式信息添加到工作簿。样式会增加一些性能开销。默认为 false
zip ExcelJS 内部传递给 Archiver 的 Zip选项。默认值为 undefined

如果在选项中未指定 streamfilename,则工作簿编写器将创建一个 StreamBuf 对象,该对象将 XLSX 工作簿的内容存储在内存中。可以通过属性 workbook.stream 访问此 StreamBuf 对象,该对象可用于通过 stream.read() 直接访问字节,或将内容通过管道传输到另一个流。

// 使用样式和共享字符串构造流式 XLSX 工作簿编写器
const options = {
  filename: './streamed-workbook.xlsx',
  useStyles: true,
  useSharedStrings: true
};
const workbook = new Excel.stream.xlsx.WorkbookWriter(options);

通常,流式 XLSX 写入器的接口与上述文档工作簿(和工作表)相同,实际上行,单元格和样式对象是相同的。

但是有一些区别…

构造

如上所示,WorkbookWriter 通常将要求在构造函数中指定输出流或文件。

提交数据

当工作表行准备就绪时,应将其提交,以便可以释放行对象和内容。通常,这将在添加每一行时完成…

worksheet.addRow({
   id: i,
   name: theName,
   etc: someOtherDetail
}).commit();

WorksheetWriter 在添加行时不提交行的原因是允许单元格跨行合并:

worksheet.mergeCells('A1:B2');
worksheet.getCell('A1').value = 'I am merged';
worksheet.getCell('C1').value = 'I am not';
worksheet.getCell('C2').value = 'Neither am I';
worksheet.getRow(2).commit(); // now rows 1 and two are committed.

每个工作表完成后,还必须提交:

// 完成添加数据。 提交工作表
worksheet.commit();

要完成 XLSX 文档,必须提交工作簿。 如果未提交工作簿中的任何工作表,则将在工作簿提交中自动提交它们。

// 完成 workbook.
await workbook.commit();
// ... 流已被写入
流式 XLSX 阅读器⬆

流式 XLSX 工作簿阅读器可以在ExcelJS.stream.xlsx命名空间中找到。

构造函数包含必需的输入参数和可选的options参数:

Argument Description
input (必需的) 指定从中读取XLSX工作簿的文件或可读流的名称
options (可选的) 指定如何处理读取解析期间发生的事件类型
options.entries 指定是否去触发事件('emit')或者不发出事件('ignore'),默认值是'emit'
options.sharedStrings 指定是否去缓存('cache')共享字符串,将其插入到相应的单元格值中,或者是否去触发('emit')或忽略('ignore')它们,在这两种情况下,单元格值都将是对共享字符串索引的引用。默认值是'cache'
options.hyperlinks 指定是否去缓存超链接('cache'),将其插入到相应的单元格值中,是否去触发('emit')或忽略('ignore')它们。默认值是'cache'
options.styles 指定是否去缓存样式('cache'),将其插入到相应的行或单元格值中,或是否忽略('忽略')它们。默认值是'cache'
options.worksheets 指定是否去触发('emit')或忽略('ignore')工作表。默认值是'emit'
const workbook = new ExcelJS.stream.xlsx.WorkbookReader('./file.xlsx');
for await (const worksheetReader of workbookReader) {
  for await (const row of worksheetReader) {
    // ...
  }
}

请注意,由于性能原因,worksheetReader返回一个行数组,而不是单独返回每一行: https://github.com/nodejs/node/issues/31979

迭代遍历所有事件⬆

工作簿上的事件是 ‘worksheet’、‘shared-strings’ 和 ‘hyperlinks’。 工作表上的事件是 ‘row’ 和 ‘hyperlinks’.

const options = {
  sharedStrings: 'emit',
  hyperlinks: 'emit',
  worksheets: 'emit',
};
const workbook = new ExcelJS.stream.xlsx.WorkbookReader('./file.xlsx', options);
for await (const {eventType, value} of workbook.parse()) {
  switch (eventType) {
    case 'shared-strings':
      // 值是共享字符串
    case 'worksheet':
      // 值是worksheetReader
    case 'hyperlinks':
      // 值是hyperlinksReader
  }
}
可读流⬆

我们强烈建议使用异步迭代,但我们也公开了流接口以实现向后兼容性。

const options = {
  sharedStrings: 'emit',
  hyperlinks: 'emit',
  worksheets: 'emit',
};
const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader('./file.xlsx', options);
workbookReader.read();

workbookReader.on('worksheet', worksheet => {
  worksheet.on('row', row => {
  });
});

workbookReader.on('shared-strings', sharedString => {
  // ...
});

workbookReader.on('hyperlinks', hyperlinksReader => {
  // ...
});

workbookReader.on('end', () => {
  // ...
});
workbookReader.on('error', (err) => {
  // ...
});

浏览器⬆

该库的一部分已被隔离,并经过测试可在浏览器环境中使用。

由于工作簿读写器的流式传输性质,因此未包括这些内容。只能使用基于文档的工作簿(有关详细信息,请参见 创建工作簿)。

例如,在浏览器中使用 ExcelJS 的代码可查看 github 中的 spec / browser 文件夹。

预捆绑⬆

以下文件已预先捆绑在一起,并包含在 dist 文件夹中。

  • exceljs.js
  • exceljs.min.js

值类型⬆

支持以下值类型。

Null 值⬆

Enum: Excel.ValueType.Null

空值表示没有值,通常在写入文件时将不存储(合并的单元格除外)。可用于从单元格中删除该值。例如:

worksheet.getCell('A1').value = null;

合并单元格⬆

Enum: Excel.ValueType.Merge

合并单元格是其值绑定到另一个“主”单元格的单元格。分配给合并单元将导致修改单元格。

数字值⬆

Enum: Excel.ValueType.Number

一个数字值。

例如:

worksheet.getCell('A1').value = 5;
worksheet.getCell('A2').value = 3.14159;

字符串值⬆

Enum: Excel.ValueType.String

一个简单的文本字符串。

例如:

worksheet.getCell('A1').value = 'Hello, World!';

日期值⬆

Enum: Excel.ValueType.Date

日期值,由 JavaScript Date 类型表示。

例如:

worksheet.getCell('A1').value = new Date(2017, 2, 15);

超链接值⬆

Enum: Excel.ValueType.Hyperlink

具有文本和链接值的 URL。

例如:

// 链接到网络
worksheet.getCell('A1').value = {
  text: 'www.mylink.com',
  hyperlink: 'http://www.mylink.com',
  tooltip: 'www.mylink.com'
};

// 内部链接
worksheet.getCell('A1').value = { text: 'Sheet2', hyperlink: '#\'Sheet2\'!A1' };

公式值⬆

Enum: Excel.ValueType.Formula

一个 Excel 公式,用于即时计算值。请注意,虽然单元格类型将为“公式”,但该单元格可能具有一个有效类型值,该值将从结果值中得出。

请注意,ExcelJS 无法处理公式以生成结果,必须提供该公式。

例如:

worksheet.getCell('A3').value = { formula: 'A1+A2', result: 7 };

单元格还支持便捷的获取器,以访问公式和结果:

worksheet.getCell('A3').formula === 'A1+A2';
worksheet.getCell('A3').result === 7;

共享公式⬆

共享的公式通过减少工作表 xml 中文本的重复来增强 xlsx 文档的压缩。范围中左上角的单元格是指定的母版,它将保留该范围内的所有其他单元格都将引用的公式。然后,其他“从属”单元格可以引用此主单元格,而不必再次重新定义整个公式。请注意,主公式将以常用的 Excel 方式转换为从属单元格,以便对其他单元格的引用将根据从属单元相对于主单元的偏移量向右下移。例如:如果主单元格A2具有引用A1的公式,则如果单元格B2共享A2的公式,则它将引用B1。

可以将主公式与该范围内的从属单元格一起分配给该单元格

worksheet.getCell('A2').value = {
  formula: 'A1',
  result: 10,
  shareType: 'shared',
  ref: 'A2:B3'
};

可以使用新的值形式将共享公式分配给单元格:

worksheet.getCell('B2').value = { sharedFormula: 'A2', result: 10 };

这指定单元格B2是将从A2中的公式派生的公式,其结果为10。

公式便捷获取器会将A2中的公式转换为B2中应具有的公式:

expect(worksheet.getCell('B2').formula).to.equal('B1');

可以使用 fillFormula 方法将共享的公式分配到工作表中:

// 将 A1 设置为起始编号
worksheet.getCell('A1').value = 1;

// 从 A1 开始以递增计数将 A2 填充到 A10
worksheet.fillFormula('A2:A10', 'A1+1', [2,3,4,5,6,7,8,9,10]);

fillFormula 也可以使用回调函数来计算每个单元格的值

// 从A1开始以递增计数将 A2 填充到 A100
worksheet.fillFormula('A2:A100', 'A1+1', (row, col) => row);

公式类型⬆

要区分真正的和转换后的公式单元格,请使用 FormulaType getter:

worksheet.getCell('A3').formulaType === Enums.FormulaType.Master;
worksheet.getCell('B3').formulaType === Enums.FormulaType.Shared;

公式类型具有以下值:

名称
Enums.FormulaType.None 0
Enums.FormulaType.Master 1
Enums.FormulaType.Shared 2

数组公式⬆

在 Excel 中表示共享公式的一种新方法是数组公式。以这种形式,主单元格是唯一包含与公式有关的任何信息的单元格。它包含 shareType ‘array’ 以及适用于其的单元格范围以及将要复制的公式。其余单元格是具有常规值的常规单元格。

注意:数组公式不会以共享公式的方式转换。因此,如果主单元A2引用A1,则从单元B2也将引用A1。

例如:

// 将数组公式分配给 A2:B3
worksheet.getCell('A2').value = {
  formula: 'A1',
  result: 10,
  shareType: 'array',
  ref: 'A2:B3'
};

// 可能没有必要填写工作表中的其余值

fillFormula 方法也可以用于填充数组公式

// 用数组公式 "A1" 填充 A2:B3
worksheet.fillFormula('A2:B3', 'A1', [1,1,1,1], 'array');

富文本值⬆

Enum: Excel.ValueType.RichText

样式丰富的文本。

例如:

worksheet.getCell('A1').value = {
  richText: [
    { text: 'This is '},
    {font: {italic: true}, text: 'italic'},
  ]
};

布尔值⬆

Enum: Excel.ValueType.Boolean

例如:

worksheet.getCell('A1').value = true;
worksheet.getCell('A2').value = false;

错误值⬆

Enum: Excel.ValueType.Error

例如:

worksheet.getCell('A1').value = { error: '#N/A' };
worksheet.getCell('A2').value = { error: '#VALUE!' };

当前有效的错误文本值为:

名称
Excel.ErrorValue.NotApplicable #N/A
Excel.ErrorValue.Ref #REF!
Excel.ErrorValue.Name #NAME?
Excel.ErrorValue.DivZero #DIV/0!
Excel.ErrorValue.Null #NULL!
Excel.ErrorValue.Value #VALUE!
Excel.ErrorValue.Num #NUM!

接口变化⬆

我们会尽一切努力创建一个良好的,一致的接口,该接口不会在版本之间不兼容,但令人遗憾的是,为了实现更大的利益,有时需要进行一些更改。

0.1.0⬆

Worksheet.eachRow⬆

Worksheet.eachRow 的回调函数中的参数已被交换和更改;它是 function(rowNumber,rowValues),现在是 function(row,rowNumber),使它的外观更像 underscore(_.each)方法,并且行对象优先于行号。

Worksheet.getRow⬆

此函数已从返回稀疏的单元格数组更改为返回 Row 对象。这样可以访问行属性,并有助于管理行样式等。

仍可通过 Worksheet.getRow(rowNumber).values; 获得稀疏的单元格值的数组。

0.1.1⬆

cell.model⬆

cell.styles 重命名为 cell.style

0.2.44⬆

从 Bluebird 切换到 Node 原生 Promise 的函数返回的 Promise 如果依赖 Bluebird 的额外功能,则可能会破坏调用代码。

为了减少这种情况的出现,在0.3.0中添加了以下两个更改:

  • 默认情况下使用功能更全且仍与浏览器兼容的 promise lib。 该库支持 Bluebird 的许多功能,但占用空间少得多。
  • 注入其他 Promise 实现的选项。有关更多详细信息,请参见配置部分。

配置⬆

ExcelJS现在支持对 Promise 库的依赖项注入。您可以通过在模块中包含以下代码来还原 Bluebird Promise。

ExcelJS.config.setValue('promise', require('bluebird'));

请注意:我已经使用 bluebird 专门测试了 ExcelJS(直到最近,这是它使用的库)。根据我所做的测试,它不适用于 Q。

注意事项⬆

Dist 文件夹⬆

在发布此模块之前,先对源代码进行编译和其他处理,然后再将它们放置在 dist/ 文件夹中。该自述文件标识两个文件-浏览器捆绑和压缩版本。除了在 package.json 中指定为 "main" 的文件外,不能保证 dist/ 文件夹的其他内容。

已知的问题⬆

使用 Puppeteer 进行测试⬆

该 lib 中包含的测试套件包括一个在无头浏览器中执行的小脚本,以验证捆绑的软件包。 在撰写本文时,其表现出该测试在 Windows Linux 子系统中不能很好地进行。

因此,可以通过存在名为 .disable-test-browser 的文件来禁用浏览器测试。

sudo apt-get install libfontconfig

拼接与合并⬆

如果任何 splice 操作影响合并的单元格,则合并组将无法正确移动

发布历史⬆

版本 变化
0.0.9
  • 数字格式
0.1.0
  • Bug 修复
    • "<" and “>” 在 xlsx 中正确呈现的文本字符
  • 更好的列控制
  • 更好的行控制
0.1.1
  • Bug 修复
    • 可以将更多文本数据正确写入xml(包括文本,超链接,公式结果和格式代码)
    • 更好的日期格式代码识别
  • 单元格字体样式
0.1.2
  • 修复了 zip 写入时潜在的竞争条件
0.1.3
  • 单元格对齐样式
  • 行高
  • 一些内部重构
0.1.5
  • Bug 修复
    • 现在可以在一本工作簿中处理10个或更多工作表
    • 正确添加并引用了 theme1.xml 文件
  • 单元格边框
0.1.6
  • Bug 修复
    • XLSX 文件中包含更兼容的 theme1.xml
  • 单元格填充
0.1.8
  • Bug 修复
    • XLSX 文件中包含更兼容的theme1.xml
    • 修复文件名大小写问题
  • 单元格填充
0.1.9
  • Bug 修复
    • 添加了 docProps 文件以满足 Mac Excel 用户
    • 修复文件名大小写问题
    • 修复工作表 ID 问题
  • 核心工作簿属性
0.1.10
  • Bug 修复
    • 处理找不到文件错误
  • CSV 文件
0.1.11
  • Bug 修复
    • 修复垂直中间对齐问题
  • 行与列样式
  • Worksheet.eachRow 支持选项参数
  • Row.eachCell 支持选项参数
  • 新的方法 Column.eachCell
0.2.0
  • Streaming XLSX Writer
    • At long last ExcelJS can support writing massive XLSX files in a scalable memory efficient manner. Performance has been optimised and even smaller spreadsheets can be faster to write than the document writer. Options have been added to control the use of shared strings and styles as these can both have a considerable effect on performance
  • Worksheet.lastRow
    • Access the last editable row in a worksheet.
  • Row.commit()
    • For streaming writers, this method commits the row (and any previous rows) to the stream. Committed rows will no longer be editable (and are typically deleted from the worksheet object). For Document type workbooks, this method has no effect.
0.2.2
  • One Billion Cells
    • Achievement Unlocked: A simple test using ExcelJS has created a spreadsheet with 1,000,000,000 cells. Made using random data with 100,000,000 rows of 10 cells per row. I cannot validate the file yet as Excel will not open it and I have yet to implement the streaming reader but I have every confidence that it is good since 1,000,000 rows loads ok.
0.2.3
  • Bug Fixes
    • Merge Cell Styles
      • Merged cells now persist (and parse) their styles.
  • Streaming XLSX Writer
    • At long last ExcelJS can support writing massive XLSX files in a scalable memory efficient manner. Performance has been optimised and even smaller spreadsheets can be faster to write than the document writer. Options have been added to control the use of shared strings and styles as these can both have a considerable effect on performance
  • Worksheet.lastRow
    • Access the last editable row in a worksheet.
  • Row.commit()
    • For streaming writers, this method commits the row (and any previous rows) to the stream. Committed rows will no longer be editable (and are typically deleted from the worksheet object). For Document type workbooks, this method has no effect.
0.2.4
  • Bug Fixes
    • Worksheets with Ampersand Names
      • Worksheet names are now xml-encoded and should work with all xml compatible characters
  • Row.hidden & Column.hidden
    • Rows and Columns now support the hidden attribute.
  • Worksheet.addRows
    • New function to add an array of rows (either array or object form) to the end of a worksheet.
0.2.6
  • Bug Fixes
    • invalid signature: 0x80014: Thanks to hasanlussa for the PR
  • Defined Names
    • Cells can now have assigned names which may then be used in formulas.
  • Converted Bluebird.defer() to new Bluebird(function(resolve, reject){}). Thanks to user Nishchit for the Pull Request
0.2.7
  • Data Validations
    • Cells can now define validations that controls the valid values the cell can have
0.2.8
  • Rich Text Value
    • Cells now support in-cell formatting - Thanks to Peter ADAM
  • Fixed typo in README - Thanks to MRdNk
  • Fixing emit in worksheet-reader - Thanks to Alan Gunning
  • Clearer Docs - Thanks to miensol
0.2.9
  • Fixed "read property ‘richText’ of undefined error. Thanks to james075
0.2.10
  • Refactoring Complete. All unit and integration tests pass.
0.2.11
  • Outline Levels. Thanks to cricri for the contribution.
  • Worksheet Properties
  • Further refactoring of worksheet writer.
0.2.12
  • Sheet Views. Thanks to cricri again for the contribution.
0.2.13
  • Fix for exceljs might be vulnerable for regular expression denial of service. Kudos to yonjah and Josh Emerson for the resolution.
  • Fix for Multiple Sheets opens in ‘Group’ mode in Excel. My bad - overzealous sheet view code.
  • Also fix for empty sheet generating invalid xlsx.
0.2.14
  • Fix for exceljs might be vulnerable for regular expression denial of service. Kudos to yonjah and Josh Emerson for the resolution.
  • Fixed Multiple Sheets opens in ‘Group’ mode in Excel again. Added Workbook views.
  • Also fix for empty sheet generating invalid xlsx.
0.2.15
  • Added Page Setup Properties. Thanks to Jackkum for the PR
0.2.16
  • New Page Setup Property: Print Area
0.2.17
  • Merged Fix a bug on phonetic characters. This fixes an issue related to reading workbooks with phonetic text in. Note phonetic text is not properly supported yet - just properly ignored. Thanks to zephyrrider and gen6033 for the contribution.
0.2.18
  • Merged Fix regression #150: Stream API fails to write XLSX files. Apologies for the regression! Thanks to danieleds for the fix.
  • Merged Fix a bug on phonetic characters. This fixes an issue related to reading workbooks with phonetic text in. Note phonetic text is not properly supported yet - just properly ignored. Thanks to zephyrrider and gen6033 for the contribution.
0.2.19
  • Merged Update xlsx.js #119. This should make parsing more resilient to open-office documents. Thanks to nvitaterna for the contribution.
0.2.20
  • Merged Changes from exceljs/exceljs#127 applied to latest version #179. Fixes parsing of defined name values. Thanks to agdevbridge and priitliivak for the contribution.
0.2.21
  • Merged color tabs for worksheet-writer #135. Modified the behaviour to print deprecation warning as tabColor has moved into options.properties. Thanks to ethanlook for the contribution.
0.2.22
  • Merged Throw legible error when failing Value.getType() #136. Thanks to wulfsolter for the contribution.
  • Honourable mention to contributors whose PRs were fixed before I saw them:
    • haoliangyu
    • wulfsolter
0.2.23
  • Merged Fall back to JSON.stringify() if unknown Cell.Type #137 with some modification. If a cell value is assigned to an unrecognisable javascript object, the stored value in xlsx and csv files will be JSON stringified. Note that if the file is read again, no attempt will be made to parse the stringified JSON text. Thanks to wulfsolter for the contribution.
0.2.24
  • Merged Protect cell fix #166. This does not mean full support for protected cells merely that the parser is not confused by the extra xml. Thanks to jayflo for the contribution.
0.2.25
  • Added functions to delete cells, rows and columns from a worksheet. Modelled after the Array splice method, the functions allow cells, rows and columns to be deleted (and optionally inserted). See Columns and Rows for details.
    Note: Not compatible with cell merges
0.2.26
  • Merged Update border-xform.js #184Border edges without style will be parsed and rendered as no-border. Thanks to skumarnk2 for the contribution.
0.2.27
  • Merged Pass views to worksheet-writer #187. Now also passes views to worksheet-writer. Thanks to Temetz for the contribution.
  • Merged Do not escape xml characters when using shared strings #189. Fixing bug in shared strings. Thanks to tkirda for the contribution.
0.2.28
  • Merged Fix tiny bug [Update hyperlink-map.js] #190Thanks to lszlkss for the contribution.
  • Merged fix typo on sheet view showGridLines option #196 “showGridlines” should have been “showGridLines”. Thanks to gadiaz1 for the contribution.
0.2.29
  • Merged Fire finish event instead of end event on write stream #199 and Listen for finish event on zip stream instead of middle stream #200. Fixes issues with stream completion events. Thanks to junajan for the contribution.
0.2.30
  • Merged Fix issue #178 #201. Adds the following properties to workbook:
    • title
    • subject
    • keywords
    • category
    • description
    • company
    • manager
    Thanks to stavenko for the contribution.
0.2.31
  • Merged Fix issue #163: the “spans” attribute of the row element is optional #203. Now xlsx parsing will handle documents without row spans. Thanks to arturas-vitkauskas for the contribution.
0.2.32
  • Merged Fix issue 206 #208. Fixes issue reading xlsx files that have been printed. Also adds “lastPrinted” property to Workbook. Thanks to arturas-vitkauskas for the contribution.
0.2.33
  • Merged Allow styling of cells with no value. #210. Includes Null type cells with style in the rendering parsing. Thanks to oferns for the contribution.
0.2.34
  • Merged Fix “Unexpected xml node in parseOpen” bug in LibreOffice documents for attributes dc:language and cp:revision #212. Thanks to jessica-jordan for the contribution.
0.2.35
  • Fixed Getting a column/row count #74. Worksheet now has rowCount and columnCount properties (and actual variants), Row has cellCount.
0.2.36
  • Merged Stream reader fixes #217. Thanks to kturney for the contribution.
0.2.37
  • Merged Fix output order of Sheet Properties #225. Thanks to keeneym for the contribution.
  • Merged remove empty worksheet[0] from _worksheets #231. Thanks to pookong for the contribution.
  • Merged do not skip empty string in shared strings so that indexes match #232. Thanks again to pookong for the contribution.
  • Merged use shared strings for streamed writes #233. Thanks again to pookong for the contribution.
0.2.38
  • Merged Add a comment for issue #216 #236. Thanks to jsalwen for the contribution.
  • Merged Start on support for 1904 based dates #237. Fixed date handling in documents with the 1904 flag set. Thanks to holm for the contribution.
0.2.39
  • Merged Stops Bluebird warning about unreturned promise #245. Thanks to robinbullocks4rb for the contribution.
  • Merged Added missing dependency: col-cache.js #247. Thanks to Manish2005 for the contribution.
0.2.42
  • Browser Compatible!
    • Well mostly. I have added a browser sub-folder that contains a browserified bundle and an index.js that can be used to generate another. See Browser section for details.
  • Fixed corrupted theme.xml. Apologies for letting that through.
  • Merged [BUGFIX] data validation formulae undefined #253. Thanks to jayflo for the contribution.
0.2.43
  • Merged added a (maybe partial) solution to issue 99. i wasn’t able to create an appropriate test #255. This fixes Too few data or empty worksheet generate malformed excel file #99. Thanks to mminuti for the contribution.
0.2.44
  • Reduced Dependencies.
    • Goodbye lodash, goodbye bluebird. Minified bundle is now just over half what it was in the first version.
0.2.45
  • Merged Sheets with hyperlinks and data validations are corrupted #256. Thanks to simon-stoic for the contribution.
0.2.46
  • Merged Exclude character controls from XML output. Fixes #234 #262. Thanks to holm for the contribution.
  • Merged Add support for identifier #259. This fixes Broken XLSX because of “vertical tab” ascii character in a cell #234. Thanks to NOtherDev for the contribution.
0.3.0
  • Addressed Breaking change removing bluebird #266. Appologies for any inconvenience.
  • Added Promise library dependency injection. See Config section for more details.
0.3.1
  • Merged Update dependencies #279. Thanks to holm for the contribution.
  • Merged Minor fixes for stream handling #267. Thanks to holm for the contribution.
  • Added automated tests in phantomjs for the browserified code.
0.4.0
  • Fixed issue Boolean cell with value =“true” is returned as 1 #278. The fix involved adding two new Call Value types:
    • Boolean Value
    • Error Value
    Note: Minor version has been bumped up to 4 as this release introduces a couple of interface changes:
    • Boolean cells previously will have returned 1 or 0 will now return true or false
    • Error cells that previously returned a string value will now return an error structure
  • Fixed issue Code correctness - setters don’t return a value #280.
  • Addressed issue v0.3.1 breaks meteor build #288.
0.4.1
  • Merged Add support for cp:contentStatus #285. Thanks to holm for the contribution.
  • Merged Fix Valid characters in XML (allow \n and \r when saving) #286. Thanks to Rycochet for the contribution.
  • Fixed hyperlink with query arguments corrupts workbook #275. The hyperlink target is not escaped before serialising in the xml.
0.4.2
  • Addressed the following issues:

    • White text and borders being changed to black #290
    • Losing formatting/pivot table from loaded file #261
    • Solid fill become black #272
    These issues are potentially caused by a bug that caused colours with zero themes, tints or indexes to be rendered and parsed incorrectly.

    Regarding themes: the theme files stored inside the xlsx container hold important information regarding colours, styles etc and if the theme information from a loaded xlsx file is lost, the results can be unpredictable and undesirable. To address this, when an ExcelJS Workbook parses an XLSX file, it will preserve any theme files it finds and include them when writing to a new XLSX. If this behaviour is not desired, the Workbook class exposes a clearThemes() function which will drop the theme content. Note that this behaviour is only implemented in the document based Workbook class, not the streamed Reader and Writer.

0.4.3
  • Merged Support error references in cell ranges #294. Thanks to holm for the contribution.
0.4.4
  • Merged Issue with copied cells #297. This merge adds support for shared formulas. Thanks to muscapades for the contribution.
0.4.6
  • Merged Correct spelling #304. Thanks to toanalien for the contribution.
  • Merged Added support for auto filters #306. This adds Auto Filters to the Worksheet. Thanks to C4rmond4i for the contribution.
  • Restored NodeJS 4.0.0 compatability by removing the destructuring code. My apologies for any inconvenience.
0.4.9
  • Switching to transpiled code for distribution. This will ensure compatability with 4.0.0 and above from here on. And it will also allow use of much more expressive JS code in the lib folder!
  • Basic Image Support!Images can now be added to worksheets either as a tiled background or stretched over a range. Note: other features like rotation, etc. are not supported yet and will reqeuire further work.
0.4.10
  • Merged Add missing Office Rels #319. Thanks goes to mauriciovillalobos for the contribution.
  • Merged Add printTitlesRow Support #320 Thanks goes to psellers89 for the contribution.
0.4.11
  • Merged Avoid error on anchor with no media #327. Thanks goes to holm for the contribution.
  • Merged Assortment of fixes for streaming read #332. Thanks goes to holm for the contribution.
0.4.12
  • Merged Don’t set address if hyperlink r:id is undefined #334. Thanks goes to holm for the contribution.
0.4.13
  • Merged Issue 296 #343. This fixes Issue with writing newlines #296. Thanks goes to holly-weisser for the contribution.
0.4.14
  • Merged Syntax highlighting added ✨ #350. Thanks goes to rmariuzzo for the contribution.
0.5.0
  • Merged Fix right to left issues #356. Fixes Add option to RTL file #72 and Adding an option to set RTL worksheet #126. Big thank you to alitaheri for this contribution.
0.5.1
  • Merged Fix #345 TypeError: Cannot read property ‘date1904’ of undefined #364. This fixes TypeError: Cannot read property ‘date1904’ of undefined #345. Thanks to Diluka for this contribution.
0.6.0
  • Merged Add rowBreaks feature. #389. Thanks to brucejo75 for this contribution.
0.6.1
  • Merged Guard null model fields - fix and tests #403. Thanks to thecjharries for this contribution. Also thanks to Ryc O’Chet for help with reviewing.
0.6.2
  • Merged Add some comments in readme according csv importing #396. Thanks to Michael Lelyakin for this contribution. Also thanks to planemar for help with reviewing. This also closes csv to stream doesn’t work #395.
0.7.0
  • Merged Impl #407. Thanks to Ocke Janssen and Kay Ramme for this contribution. This change allows control on how images are anchored to cells.
0.7.1
  • Merged Don’t break when attempting to import a zip file that’s not an Excel file (eg. .numbers) #423. Thanks to Andreas Lind for this contribution. This change makes exceljs more reslilient when opening non-excel files.
  • Merged Fixes #419 : Updates readme. #434. Thanks to Vishnu Kyatannawar for this contribution.
  • Merged Don’t break when docProps/core.xml contains a tag #436. Thanks to Andreas Lind for this contribution. This change handles core.xml files with empty version tags.
0.8.0
  • Merged Add Base64 Image support for the .addImage() method #442. Thanks to James W Mann for this contribution.
  • Merged update moment to 2.19.3 #453. Thanks to Markan Patel for this contribution.
0.8.1
  • Merged Additional information about font family property #457. Thanks to kayakyakr for this contribution.
  • Merged Fixes #458 #459. This fixes Add style to column causes it to be hidden #458. Thanks to Alexander James Phillips for this contribution.
0.8.2
  • Merged Don’t break when loading an Excel file containing a chartsheet #466. Thanks to Andreas Lind for this contribution.
  • Merged Hotfix/sheet order#257 #471. This fixes Sheet Order #257. Thanks to Robbi for this contribution.
0.8.3
  • Assimilated fix #79 outdated dependencies in unzip2. Thanks to Jules Sam. Randolph for starting this fix and a really big thanks to Alexander Kachkaev for finding the final solution.
0.8.4
  • Merged Round Excel date to nearest millisecond when converting to javascript date #479. Thanks to Benoit Jean for this contribution.
0.8.5
  • Merged Bug fix: wb.worksheets/wb.eachSheet caused getWorksheet(0) to return sheet #485. Thanks to mah110020 for this contribution.
0.9.0
  • Merged Feature/issue 424 #489. This fixes No way to control summaryBelow or summaryRight #424. Many thanks to Sarah for this contribution.
0.9.1
  • Merged add type definition #490. This adds type definitions to ExcelJS! Many thanks to taoqf for this contribution.
1.0.0
  • Merged Add Node 8 and Node 9 to continuous integration testing #494. Many thanks to Markan Patel for this contribution.
  • Merged Small README fix #508. Many thanks to Guilherme Bernal for this contribution.
  • Merged Add support for inlineStr, including rich text #501. Many thanks to linguamatics-pdenes and Rob Scott for their efforts towards this contribution. Since this change is technically a breaking change (the rendered XML for inline strings will change) I’m making this a major release!
1.0.1
  • Fixed spliceColumns problem when the number of columns are important #520.
1.0.2
  • Merged Loosen exceljs’s dependency requirements for moment #524. Many thanks to nicoladefranceschi for this contribution. This change addresses Ability to use external “moment” package #517.
1.1.0
  • Addressed Is there a way inserting values in columns. #514. Added a new getter/setter property to Column to get and set column values (see Columns for details).
1.1.1
  • Merged Include index.d.ts in published packages #532. To fix TypeScript definitions missing from npm package #525. Many thanks to Kagami Sascha Rosylight for this contribution.
1.1.2
  • Merged Don’t break when docProps/core.xml contains #536. Many thanks to Andreas Lind (and reviewers) for this contribution.
1.1.3
  • Merged Try to handle the case where a element is missing an r attribute #537. Many thanks to Andreas Lind for this contribution.
1.2.0
  • Merged Add dateUTC flag to CSV Writing #544. Many thanks to Zackery Griesinger for this contribution.
1.2.1
  • Merged worksheet name is writable #547. Many thanks to xzper for this contribution.
1.3.0
  • Merged Add CSV write buffer support #549. Many thanks to Jarom Loveridge for this contribution.
1.4.2
  • Merged Discussion: Customizable row/cell limit #541. Many thanks to Andreas Lind for this contribution.
1.4.3
  • Merged Get the right text out of hyperlinked formula cells #552. Many thanks to Andreas Lind and Christian Holm for this contribution.
1.4.5
  • Merged Add test case with a huge file #556. Many thanks to Andreas Lind and Christian Holm for this contribution.
1.4.6
  • Merged Update README.md to reflect correct functionality of row.addPageBreak() #557. Many thanks to RajDesai for this contribution.
  • Merged fix index.d.ts #558. Many thanks to Diluka for this contribution.
1.4.7
  • Merged List /xl/sharedStrings.xml in [Content_Types].xml only if one of the … #562. Many thanks to Priidik Vaikla for this contribution.
1.4.8
  • Merged List /xl/sharedStrings.xml in [Content_Types].xml only if one of the … #562. Many thanks to Priidik Vaikla for this contribution.
  • Fixed issue with above where shared strings were used but the content type was not added.
1.4.9
  • Merged List /xl/sharedStrings.xml in [Content_Types].xml only if one of the … #562. Many thanks to Priidik Vaikla for this contribution.
  • Fixed issue with above where shared strings were used but the content type was not added.
  • Fixed issue 1.4.8 broke writing Excel files with useSharedStrings:true #581.
1.4.10
  • Merged core-xform: Tolerate a missing cp: namespace for the coreProperties element #564. Many thanks to Andreas Lind for this contribution.
1.4.12
  • Merged Avoid error on malformed address #567. Many thanks to Andreas Lind for this contribution.
  • Merged Added a missing Promise in index.d.ts #571. Many thanks to Gabriel Fournier for this contribution. This release should fix Is workbook.commit() still a promise or not #548
1.4.13
  • Merged Issue #488 #574. Many thanks to dljenkins for this contribution. This release should fix Invalid time value Exception #488.
1.5.0
  • Merged Sheet add state for hidden or show #577. Many thanks to Freddie Hsinfu Huang for this contribution. This release should fix hide worksheet and reorder sheets #226.
1.5.1
  • Merged Update index.d.ts #582. Many thanks to hankolsen for this contribution.
  • Merged Decode the x<4 hex chars> escape notation in shared strings #584. Many thanks to Andreas Lind for this contribution.
1.6.0
  • Added .html property to Cells to facilitate html-safe rendering. See Handling Individual Cells for details.
1.6.1
  • Merged Fix Issue #488 where dt is an invalid date format. #587 to fix Invalid time value Exception #488. Many thanks to Iliya Zubakin for this contribution.
1.6.2
  • Merged Fix Issue #488 where dt is an invalid date format. #587 to fix Invalid time value Exception #488. Many thanks to Iliya Zubakin for this contribution.
  • Merged drawing element must be below rowBreaks according to spec or corrupt worksheet #590 Many thanks to Liam Neville for this contribution.
1.6.3
  • Merged set type optional #595 Many thanks to taoqf for this contribution.
  • Merged Fix some xlsx stream read xlsx not in guaranteed order problem #578 Many thanks to KMethod for this contribution.
  • Merged Fix formatting issue in README #599 Many thanks to Vishnu Kyatannawar for this contribution.
1.7.0
  • Merged Ability to set tooltip for hyperlink #602 Many thanks to Kuznetsov Aleksey for this contribution.
1.8.0
  • Merged Fix misinterpreted ranges from #636 Many thanks to Andreas Lind for this contribution.
  • Merged Add LGTM code quality badges #640 Many thanks to Xavier RENE-CORAIL for this contribution.
  • Merged Add type definition for Column.values #646 Many thanks to Emil Laine for this contribution. This fixes Column.values is missing TypeScript definitions #645.
  • Merged Update README.md with load() option #663 Many thanks to Joanna Walker for this contribution.
  • Merged fixed packages according to npm audit #677 Many thanks to Manuel Minuti for this contribution.
  • Merged Update index.d.ts #699 Many thanks to Ray Yen for this contribution.
  • Merged Replaced node-unzip-2 to unzipper package which is more robust #708 Many thanks to johnmalkovich100 for this contribution.
  • Merged Read worksheet hidden state #728 Many thanks to Dishu(Lester) Lyu for this contribution.
  • Merged add Worksheet.state typescript definition fix #714 #736 Many thanks to Ilyes Kechidi for this contribution. This fixes Worksheet State does not exist in index.d.ts #714.
1.9.0
  • Merged Improvements for images (correct reading/writing possitions) #702. This fixes Image location don’t respect Column width #650 and Image position - stretching image #467. Many thanks to Siemienik Paweł for this contribution.
1.9.1
  • Merged Add Typescript support for formulas without results #619. Many thanks to Loursin for this contribution.
  • Merged Fix existing row styles when using spliceRows #737. Many thanks to cxam for this contribution.
  • Merged Consistent code quality #774. Many thanks to Andreas Lubbe for this contribution.
1.10.0
  • Fixed effect of splicing rows and columns on defined names
  • Merged Add support for adding images anchored to one cell #746. Many thanks to Karl von Randow for this contribution.
  • Merged Add vertical align property #758. Many thanks to MikeZyatkov for this contribution.
  • Merged Replace the temp lib to tmp #775. Many thanks to Ivan Sotnikov for this contribution.
  • Merged Replace the temp lib to tmp #775. Many thanks to Andreas Lubbe for this contribution.
  • Merged Update Worksheet.dimensions return type #793. Many thanks to Siemienik Paweł for this contribution.
  • Merged One more types fix #795. Many thanks to Siemienik Paweł for this contribution.
1.11.0
  • Merged Add the ability to bail out of parsing if the number of columns exceeds a given limit #776. Many thanks to Andreas Lind for this contribution.
  • Merged Add support for repeated columns on every page when printing. #799. Many thanks to Jasmin Auger for this contribution.
  • Merged Do not use a promise polyfill on modern setups #815. Many thanks to Andreas Lubbe for this contribution.
  • Merged copy LICENSE to the dist folder #807. Many thanks to Yuping Zuo for this contribution.
  • Merged Avoid unhandled rejection on XML parse error #813. Many thanks to Andreas Lind for this contribution.
1.12.0
  • Merged (chore) increment unzipper to 0.9.12 to address npm advisory 886 #819. Many thanks to Kreig Zimmerman for this contribution.
  • Merged docs(README): improve docs #817. Many thanks to Yuping Zuo for this contribution.
  • Merged add comment support #529 #823. Many thanks to ilimei for this contribution.

    This fixes the following issues:

    • Is it possible to add comment on a cell? #202
    • Add comment to cell #451
    • Excel add comment on cell #503
    • How to add Cell comment #529
    • Please add example to how I can insert comments for a cell #707
1.12.1
  • Merged fix issue with print area defined name corrupting file #822. Many thanks to Julia Donaldson for this contribution. This fixes issue Defined Names Break/Corrupt Excel File into Repair Mode #664.
  • Merged Only keep at most 31 characters for sheetname #831. Many thanks to Xuebin He for this contribution. This fixes issue Limit worksheet name length to 31 characters #398.
1.12.2
  • Merged add cn doc #834 and update cn doc #852. Many thanks to flydragon for this contribution.
  • Merged fix minor spelling mistake in readme #853. Many thanks to John Varga for this contribution.
  • Merged Fix defaultRowHeight not working #855. Many thanks to autukill for this contribution. This should fix row height doesn’t apply to row #422, The worksheet.properties.defaultRowHeight can’t work!! How to set the rows height, help!! #634 and Default row height doesn’t work ? #696.
  • Merged Always keep first font #854. Many thanks to Dmitriy Gusev for this contribution. This should fix document scale (width only) is different after read & write #816, Default font from source document can not be parsed. #833 and Wrong base font: hardcoded Calibri instead of font from the document #849.
1.13.0
  • Merged zip: allow tuning compression for performance or size #862. Many thanks to myfreeer for this contribution.
  • Merged Feat configure headers and footers #863. Many thanks to autukill for this contribution.
  • Fixed an issue with defaultRowHeight where the default value resulted in ‘customHeight’ property being set.
1.14.0
  • Merged Fix header and footer text format error in README.md #874. Many thanks to autukill for this contribution.
  • Added Tables. See Tables for details.
  • Merged fix: #877 and #880. Many thanks to Alexander Heinrich for this contribution. This fixes bug: Hyperlink without text crashes write #877 and bug: malformed comment crashes on write #880
1.15.0
  • Merged Add Compression level option to WorkbookWriterOptions for streaming #889. Many thanks to Alfredo Benassi for this contribution.
  • Merged Feature/Cell Protection #903 and Feature/Sheet Protection #907. Many thanks to karabaesh for these contributions.
2.0.1

Major Version Change

Introducing async/await to ExcelJS!

The new async and await features of JavaScript can help a lot to make code more readable and maintainable. To avoid confusion, particularly with returned promises from async functions, we have had to remove the Promise class configuration option and from v2 onwards ExcelJS will use native Promises. Since this is potentially a breaking change we’re bumping the major version for this release.

Changes

  • Merged Introduce async/await #829. Many thanks to Andreas Lubbe for this contribution.
  • Merged Update index.d.ts #930. Many thanks to cosmonovallc for this contributions.
  • Merged TS: Add types for addTable function #940. Many thanks to egmen for this contributions.
  • Merged added explicit return types to the type definitions of Worksheet.protect() and Worksheet.unprotect() #926. Many thanks to Tamas Czinege for this contributions.
  • Dropped dependencies on Promise libraries.
3.0.0

Another Major Version Change

Javascript has changed a lot over the years, and so have the modules and technologies surrounding it. To this end, this major version of ExcelJS changes the structure of the publish artefacts:

Main Export is now the Original Javascript Source

Prior to this release, the transpiled ES5 code was exported as the package main. From now on, the package main comes directly from the lib/ folder. This means a number of dependencies have been removed, including the polyfills.

ES5 and Browserify are Still Included

In order to support those that still require ES5 ready code (e.g. as dependencies in web apps) the source code will still be transpiled and available in dist/es5.

The ES5 code is also browserified and available as dist/exceljs.js or dist/exceljs.min.js

See the section Importing for details

3.1.0
  • Merged Uprev fast-csv to latest version which does not use unsafe eval #873. Many thanks to Mike Townsend for this contribution.
  • Merged Exclude Infinity on createInputStream #906. Many thanks to Sophie Kwon for this contribution.
  • Merged Feature/Add comments/notes to stream writer #911. Many thanks to brunoargolo for this contribution. This fixes Can’t add cell comment using streaming WorkbookWriter #868
  • Fixed an issue with reading .xlsx files containing notes. This should resolve the following issues:
    • Reading comment/note from xlsx #941
    • Excel.js doesn’t parse comments/notes. #944
3.2.0
  • Merged Add document for zip options of streaming WorkbookWriter #923. Many thanks to Soichi Takamura for this contribution.
  • Merged array formula #933. Many thanks to yoann-antoviaque for this contribution. This fixes broken array formula #932 and adds Array Formulae to ExcelJS.
3.3.0
  • Merged Fix anchor.js #892. Many thanks to Wojciech Wojtkowski for this contribution.
  • Merged add xml:space=“preserve” for all whitespaces #896. Many thanks to Sebastian Keller for this contribution.
  • Merged Add shrinkToFit to document and typing #959. Many thanks to (‘3’) for this contribution. This fixes shrinkToFit property not on documentation #943.
  • Merged #951: Force formula re-calculation on file open from Excel #980. Many thanks to zymon for this contribution. This fixes Force formula re-calculation on file open from Excel #951.
  • Fixed Lib contains class syntax, not compatible with IE11 #989.
3.3.1
  • Merged Add headerFooter to worksheet model when importing from file #1000. Many thanks to Kaiichiro Ota for this contribution.
  • Merged Update eslint plugins and configs #1005, Drop grunt-lib-phantomjs #1006 and Rename .browserslintrc.txt to .browserslistrc #1007. Many thanks to Takeshi Kurosawa for this contribution.
  • Merged Fix issue #988 #1012. This fixes Can not read excel file #988. Many thanks to Todd Hambley for this contribution.
3.4.0
  • Merged Feature/stream writer add background images #1016. Many thanks to brunoargolo for this contribution.
  • Merged Fix issue # 991 #1019. This fixes read csv file issue #991. Many thanks to Nathaniel J. Liberty for this contribution.
  • Merged Large excels - optimize performance of writing file by excelJS + optimize generated file (MS excel opens it much faster) #1018. Many thanks to Piotr for this contribution.
3.5.0
  • Conditional Formatting A subset of Excel Conditional formatting has been implemented! Specifically the formatting rules that do not require XML to be rendered inside an node, or in other words everything except databar and three icon sets (3Triangles, 3Stars, 5Boxes). These will be implemented in due course
  • Merged remove core-js/ import #1030. Many thanks to jeffrey n. carre for this contribution. This change is used to create a new browserified bundle artefact that does not include any polyfills. See Browserify for details.
3.6.0
  • Merged 1041 multiple print areas #1042. Many thanks to Alexander Pruss for this contribution.
  • Merged fix typings for cell.note #1058. Many thanks to xydens for this contribution.
  • Conditional Formatting has been completed. The conditional formattings including dataBar and the three iconSet types (3Triangles, 3Stars, 5Boxes) are now available.
3.6.1
  • Merged Clarify merging cells by row/column numbers #1047. Many thanks to Kendall Roth for this contribution.
  • Merged Fix README mistakes concerning freezing views #1048. Many thanks to overlookmotel for this contribution.
  • Merged:
    • fix issue #1045 horizontalCentered & verticalCentered in page not working #1073
    • Fix the problem of anchor failure of readme_zh.md file #1082
    • Fix problems caused by case of worksheet names #1065
    Many thanks to Alan Wang for this contribution.
3.7.0
  • Merged Fix Issue #1075: Unable to read/write defaultColWidth attribute in node #1076. Many thanks to Kaiichiro Ota for this contribution.
  • Merged function duplicateRows added #1078 and Duplicate rows #1088. Many thanks to cbeltrangomez84 for this contribution.
  • Merged Prevent from unhandled promise rejection durning workbook load #1087. Many thanks to Wojtek for this contribution.
  • Merged fix issue #899 Support for inserting pictures with hyperlinks #1071. Many thanks to Alan Wang for this contribution.
  • Merged Update TS definition to reference proper internal libraries #1089. Many thanks to Jesse Kawell for this contribution.
3.8.0
  • Merged Issue/Corrupt workbook using stream writer with background image #1090. Many thanks to brunoargolo for this contribution.
  • Merged Fix index.d.ts #1092. Many thanks to Siemienik Paweł for this contribution.
  • Merged Wait for writing to tmp fiels before handling zip stream close #1093. Many thanks to Wojtek for this contribution.
  • Merged Support ArrayBuffer as an xlsx.load argument #1095. Many thanks to Wojtek for this contribution.
  • Merged Export shared strings with RichText #1099. Many thanks to Kaiichiro Ota for this contribution.
  • Merged Keep borders of merged cells after rewriting an Excel workbook #1102. Many thanks to Kaiichiro Ota for this contribution.
  • Merged Fix #1103: editAs not working #1104. Many thanks to Alan Wang for this contribution.
  • Merged Fix to issue #1101 #1105. Many thanks to Carlos Andres Beltran Gomez for this contribution.
  • Merged fix some errors and typos in readme #1107. Many thanks to Alan Wang for this contribution.
  • Merged Update issue templates #1112. Many thanks to Siemienik Paweł for this contribution.
3.8.1
  • Merged Update issue templates #1112. Many thanks to Siemienik Paweł for this contribution.
  • Merged Typo: Replace ‘allways’ with ‘always’ #1124. Many thanks to Siemienik Paweł for this contribution.
  • Merged Replace uglify with terser #1125. Many thanks to Andreas Lubbe for this contribution.
  • Merged Apply codestyles on each commit and run lint:fix #1126. Many thanks to Andreas Lubbe for this contribution.
  • Merged [WIP] Replace sax with saxes #1127. Many thanks to Andreas Lubbe for this contribution.
  • Merged Add PR, Feature Request and Question github templates #1128. Many thanks to Andreas Lubbe for this contribution.
  • Merged fix issue #749 Fix internal link example errors in readme #1137. Many thanks to Alan Wang for this contribution.
3.8.2
  • Merged Update @types/node version to latest lts #1133. Many thanks to Siemienik Paweł for this contribution.
  • Merged fix issue #1118 Adding Data Validation and Conditional Formatting to the same sheet causes corrupt workbook #1134. Many thanks to Alan Wang for this contribution.
  • Merged Add benchmarking #1139. Many thanks to Andreas Lubbe for this contribution.
  • Merged fix issue #731 image extensions not be case sensitive #1148. Many thanks to Alan Wang for this contribution.
  • Merged fix issue #1165 and update index.d.ts #1169. Many thanks to Alan Wang for this contribution.

你可能感兴趣的:(ExcelJS 使用帮助文档)