@atomictech/xlsx-write-stream

中文文档
2.0.2发布于 2021/06/26Apache-2.0ESM + CJS近 30 天下载 1.2万

Stream huge amount of data into an XLSX generated file stream with minimum memory footprint.

原始使用文档
文档目录15 节
  1. Table of content
  2. 📦 Install
  3. 🦄 Usage
  4. Basic pipe
  5. Basic write
  6. Custom styles
  7. 🔧 API
  8. XLSXWriteStream ⇐ Transform
  9. new XLSXWriteStream([options])
  10. StyleDefs
  11. 🚧 Compatibility
  12. 👥 Authors
  13. 🤝 Contributing
  14. ⭐️ Show your support
  15. 📝 License

⏩ XLSX Write Stream ⏩

Build Status Version Documentation Maintenance License: Apache-2.0

Stream huge amount of data into an XLSX generated file stream with minimum memory footprint.

XLSX Write Stream is a streaming writer for XLSX spreadsheets. Its purpose is to replace CSV for large exports, because using CSV in Excel is very buggy and error prone. It's very efficient and can quickly write hundreds of thousands of rows with low memory usage.

Table of content

📦 Install

npm install @atomictech/xlsx-write-stream

🦄 Usage

For all usages keep in mind that you should feed your XLSXWriteStream instance one row at a time, where a row could be an array or an object.

const row = [1, '02', new Date('2015-10-21T16:29:00.000Z'), true, false, '🦄'];

or

const row = {
  'A Number Column': 1,
  'A Text Column': '02',
  'A Date Column': new Date('2015-10-21T16:29:00.000Z'),
  'A Boolean Column': true,
  'Another Boolean Column': false,
  'Another Text Column': '🦄'
};

Basic pipe

import XLSXWriteStream from '@atomictech/xlsx-write-stream';

// Initialize the writer
const xlsxWriter = new XLSXWriteStream();

// Pipe a Stream.Readable input stream into the writer
const inputStream = new MyCustomReadableStream();
inputStream.pipe(xlsxWriter);

// Pipe the writer into a Stream.Writable output stream in order to retrieve XLSX file data,
// write it into file or send it as HTTP response.
const writeStream = fs.createWriteStream('file.xlsx');
xlsxWriter.pipe(writeStream);

Basic write

import XLSXWriteStream from '@atomictech/xlsx-write-stream';

// [optional] Define some header
const columns = ['A Number Column', 'A Text Column', 'A Date Column', 'A Boolean Column', 'Another Boolean Column', 'Another Text Column'];

// Initialize the writer
const xlsxWriter = new XLSXWriteStream({ header: true, columns });

// Pipe the writer into a Stream.Writable output stream in order to retrieve XLSX file data,
// write it into file or send it as HTTP response.
const writeStream = fs.createWriteStream('file.xlsx');
xlsxWriter.pipe(writeStream);

// Write rows one by one with
const row = [1, '02', new Date('2015-10-21T16:29:00.000Z'), true, false, '🦄'];
xlsxWriter.write(row);
xlsxWriter.end(); // Do not forget to end the stream!

⚠️ Caution ⚠️

If you are not familiar with streams, in order to take advantage of the smallest memory footprint possible with this form, you need to be aware of backpressure concept and use the boolean returned by the write function!

See explanations here: writable.write(chunk[, encoding][, callback])

Custom styles

An options.styleDefs parameter is available in order to redefine type style formats.

import { TypeStyleKey } from '@atomictech/xlsx-write-stream';

// Declare custom styles definitions
const styleDefs = {};
styleDefs[TypeStyleKey.DATE] = { formatCode: 'yy-mm-dd hh:mm' };
styleDefs[TypeStyleKey.INT] = { numFmtId: 49 }; // 49 is "enforced text format"

// Create the writer
const xlsxWriter = new XLSXWriterStream({ styleDefs });

// NB: if you set `format: false` your styleDefs will not be used

🔧 API

XLSXWriteStream ⇐ Transform

new XLSXWriteStream([options])

Create new stream transform that handles Array or Object as input chunks. Be aware that first row chunk is determinant in the transform configuration process for further row chunks.

ParamTypeDefaultDescription
[options]Object
[options.header]BooleanfalseDisplay the column names on the first line if the columns option is provided or discovered.
[options.columns]Array|ObjectList of properties when records are provided as objects. Work with records in the form of arrays based on index position; order matters. Auto discovered in the first record when the user write objects, can refer to nested properties of the input JSON, see the header option on how to print columns names on the first line.
[options.format]BooleantrueIf set to false writer will not format cells with number, date, boolean and text.
[options.styleDefs]StyleDefsIf set you can overwrite default standard type styles by other standard ones or even define custom formatCode.
[options.immediateInitialization]BooleanfalseIf set to true writer will initialize archive and start compressing xlsx common stuff immediately, adding subsequently a little memory and processor footprint. If not, initialization will be delayed to the first data processing.

StyleDefs

A little of TypeScript to explain StyleDefs interface:

enum TypeStyleKey = {
  NUMBER: 'default', //!\\ Unused in the actual type conversion
  INT: 'int', // Integer <1000
  FLOAT: 'float', // Float <1000
  BIG_INT: 'bigInt', // Integer >=1000
  BIG_FLOAT: 'bigFloat', // Float >=1000
  EXP_NUMBER: 'expNumber', // Number with more than 10 digits/characters (ex: 10000000000 or 12.45678901)
  TEXT: 'text', // String
  DATE: 'date', // Date
  DATETIME: 'datetime' //!\\ Unused in the actual type conversion
};

interface TypeFormatReference {
  numFmtId: number;
}

interface TypeFormatDefinition {
  formatCode: string;
}

interface StyleDefs {
    [typeKey: TypeStyleKey]: TypeFormatReference | TypeFormatDefinition;
}

Example:

{
  date: { formatCode: 'yy-mm-dd hh:mm' },
  int: { numFmt: 49 }
}

See here for other default numFmtId: https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-2.8.1

🚧 Compatibility

XLSX Write Stream - supported:

Cell type:

  • string [starting with =] ➡ formula
  • string [others] ➡ text
  • date ➡ date
  • number ➡ number
  • boolean ➡ boolean

Cell type formatting:

  • text (default: numFmtId: 49 - enforce text even if could be interpreted as number)
  • date (default: formatCode: 'yyyy-mm-dd' - )
  • number
    • int (default: numFmtId: 1)
    • float (default: numFmtId: 2)
    • bigInt (default: numFmtId: 3)
    • bigFloat (default: numFmtId: 4)
    • expNumber (default: numFmtId: 1)

XLSX Write Stream - NOT supported:

  • charts
  • comments
  • ... and a myriad of other OOXML features. It's strictly a CSV replacement.

👥 Authors

👤 Apify

👤 AtomicTech

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.

⭐️ Show your support

Give a ⭐️ if this project helped you!

📝 License

This project is Apache-2.0 licensed.

版本与发布记录

@atomictech/xlsx-write-stream 共发布 8 个版本,首次发布于 2020/03/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.2latest2021/06/265 年前
2.0.12021/02/085 年前
2.0.02020/08/266 年前
1.1.22020/07/296 年前
1.1.12020/07/206 年前
1.1.02020/03/276 年前

关于 @atomictech/xlsx-write-stream 的常见问题

@atomictech/xlsx-write-stream 是什么?

Stream huge amount of data into an XLSX generated file stream with minimum memory footprint.

如何安装 @atomictech/xlsx-write-stream?

在项目目录运行 npm install @atomictech/xlsx-write-stream,或使用 pnpm add / yarn add / bun add。

@atomictech/xlsx-write-stream 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atomictech__xlsx-write-stream 是否存在。

@atomictech/xlsx-write-stream 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@atomictech/xlsx-write-stream 可以商用吗?

@atomictech/xlsx-write-stream 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@atomictech/xlsx-write-stream 的下载量有多少?

根据 npm 官方下载统计,@atomictech/xlsx-write-stream 在 2026/10/07 – 2026/11/05 这 30 天内被下载 11,983 次,日均约 399 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atomictech/xlsx-write-stream 还在维护吗?

最新版本 2.0.2 发布于 2021/06/26(5 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。