XLSX 写流
将大量数据流流到XLSX生成的文件流中,最小的内存足迹.
XLSX Write Stream是XLSX电子表格的流媒体写作机. 其宗旨是用CSV来取代大宗出口,因为Excel中使用CSV非常出错和容易出错. 它非常高效,可以快速地以低内存用量来写出数十万行.
目录
QQ 安装
npm install @atomictech/xlsx-write-stream
使用量
所有用法都记住,你应该养活自己 XLSXWriteStream 实例,其中一行可以是数组或对象.
const row = [1, '02', new Date('2015-10-21T16:29:00.000Z'), true, false, '🦄'];
或者说
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': '🦄'
};
基本管道
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);
基本写法
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!
⚠️ 谨慎 ⚠️
如果您不熟悉溪流,为了利用这个表单可能最小的内存足迹,您需要意识到后压概念,并使用由写入函数返回的布尔!
自定义样式
一个 options.styleDefs 参数可以重新定义类型样式格式.
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
XLSX Write 结构化 Transform
新建 XLSXWrite 结构( [选项])
创建新流转换, 将矩阵或对象作为输入块处理 . 注意第一行块是更多行块的变换配置过程中的决定因素.
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| [可选 | Object | ||
| [选项.头 | Boolean | false | 如果提供或发现列选项,则在一行显示列名. |
| [选项.列] | Array|Object | 作为对象提供记录时的属性列表。 基于索引位置的数组形式的记录;顺序事项。 在第一个记录中发现的自动,当用户写入对象时,可以指输入JSON的嵌入属性,参见: header 选项,用于打印第一行的列名. | |
| [选项.format] | Boolean | true | 如果设置为假写器, 则不会将单元格格式化为数字、 日期、 布尔和文本 . |
| [选项.styleDefs] | StyleDefs | 如果设定了, 您可以用其它标准样式覆盖默认的标准类型样式, 甚至定义自定义 formatCode。 。 。 . | |
| [选项.即刻] | Boolean | false | 如果设定为真写器, 将初始化归档并立即开始压缩 xlsx 常见内容, 并随后添加一些内存和处理器脚印 。 否则,初始化将推迟到第一次数据处理. |
样式
用于解释 StyleDefs 界面的一点类型脚本 :
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;
}
示例
{
date: { formatCode: 'yy-mm-dd hh:mm' },
int: { numFmt: 49 }
}
其他默认数字FmtId请参见此 : https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-2.8.1 页面存档备份,存于互联网档案馆
兼容性
XLSX 写入流 - 支持 :
单元格类型 :
- 字符串 [开始于
=[ 公式 - 字符串 [其他] qq文本
- 日期 + 日期
- 编号 编号
- 布尔 qq 布尔
单元格类型格式 :
- 文本( 默认 ):
numFmtId: 49- 执行案文,即使可以解释为数目) - 日期( 默认 ):
formatCode: 'yyyy-mm-dd'- ) - 说吧 - 编号
- int( 默认 ):
numFmtId: 1(中文(简体) ) - 浮点( 默认 ):
numFmtId: 2(中文(简体) ) - bigInt( 默认 ):
numFmtId: 3(中文(简体) ) - 大浮点数( 默认 ):
numFmtId: 4(中文(简体) ) - exp 编号( 默认 ):
numFmtId: 1(中文(简体) )
- int( 默认 ):
XLSX 写入流 - 没有 支持:
- 图表
- 评论
- ...和众多其他OOXML特性. 严格来说是CSV替换.
作者数
👤 说明
👤 原子技术
- 吉图布: @ 原子技术
捐款
欢迎发言、问题和特稿请求!
随便你看看 问题页面。您还可以查看 贡献指南。 。 。 .
QQ 显示支持
如果这个计划能帮助你的话,就给你一个!
许可证
这个项目是 阿帕奇 - 2.0 有执照.