jsxlsb 0.0.0-stage → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +350 -2
- package/dist/api/XlsbReader.d.ts +61 -0
- package/dist/api/interfaces.d.ts +14 -0
- package/dist/core/biff12/Biff12Reader.d.ts +54 -0
- package/dist/core/biff12/Biff12Writer.d.ts +41 -0
- package/dist/core/biff12/CellCodec.d.ts +23 -0
- package/dist/core/biff12/RecordTypes.d.ts +66 -0
- package/dist/core/biff12/VarInt.d.ts +15 -0
- package/dist/core/cell/CellData.d.ts +28 -0
- package/dist/core/cell/CellType.d.ts +9 -0
- package/dist/core/env/EnvDetector.d.ts +5 -0
- package/dist/core/limits.d.ts +36 -0
- package/dist/core/sheet/SheetReader.d.ts +61 -0
- package/dist/core/sst/SharedStringsTable.d.ts +15 -0
- package/dist/core/style/StylesReader.d.ts +12 -0
- package/dist/core/style/StylesWriter.d.ts +18 -0
- package/dist/core/template/SheetParser.d.ts +42 -0
- package/dist/core/template/TemplateReader.d.ts +21 -0
- package/dist/core/workbook/WorkbookReader.d.ts +8 -0
- package/dist/core/workbook/WorkbookWriter.d.ts +10 -0
- package/dist/core/xml/ContentTypes.d.ts +6 -0
- package/dist/core/xml/RelsGenerator.d.ts +5 -0
- package/dist/core/xml/XmlGenerator.d.ts +7 -0
- package/dist/render/canvas/CanvasConfig.d.ts +17 -0
- package/dist/render/canvas/CanvasRenderer.d.ts +47 -0
- package/dist/render/html/HtmlConfig.d.ts +10 -0
- package/dist/render/html/HtmlRenderer.d.ts +10 -0
- package/dist/render/html/HtmlStyles.d.ts +37 -0
- package/dist/utils/ColumnUtils.d.ts +2 -0
- package/dist/utils/DateUtils.d.ts +13 -0
- package/dist/utils/FormatUtils.d.ts +5 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 itcraft.cn(hellyguo)
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,351 @@
|
|
|
1
|
-
#
|
|
1
|
+
# jsxlsb
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**中文** | [English](README_en.md)
|
|
4
|
+
|
|
5
|
+
纯 JavaScript 实现的 XLSB (Excel Binary Workbook) 格式读写库。
|
|
6
|
+
|
|
7
|
+
> **线程模型(重要)**:本库运行在**单个 JavaScript 事件循环线程**上,不提供内部锁或线程池。`XlsbWriter` / `XlsbReader` 实例不可跨 Worker/线程并发共享;需要并行时,每个 Worker 使用独立实例和独立输出。文中性能数据均为**单线程**测量结果。
|
|
8
|
+
|
|
9
|
+
## 设计定位
|
|
10
|
+
|
|
11
|
+
本库的出发点始终明确:**做高性能的数据输出(写入)**。读取最初只是为了读模板;但读一旦做了,高性能读也就顺理成章地成为同一内核的自然延伸。
|
|
12
|
+
|
|
13
|
+
站在这个窄应用视角看,XLSX 其实并不需要——XLSB 已经非常能打。
|
|
14
|
+
|
|
15
|
+
## 聚焦场景
|
|
16
|
+
|
|
17
|
+
本库的适用范围只有两类场景,其余不做承诺。
|
|
18
|
+
|
|
19
|
+
**一、数据导出。** 把内存中的数据,或来自数据库游标、文件、消息流的数据写成 XLSB。特征是行数大、列数固定、样式少,对耗时与内存敏感。对应能力:批量写入、流式写入、压缩级别可调、行列上限策略。
|
|
20
|
+
|
|
21
|
+
**二、基于模板的数据导出。** 业务方在 Excel/WPS 里维护模板,模板带表头、样式、合并单元格与固定列宽;代码只在约定位置填数,其余内容原样保留。对应能力:读取模板、坐标填充、标记填充、流式填充。
|
|
22
|
+
|
|
23
|
+
不服务的场景:公式计算、图表、图片、条件格式、数据透视、宏/VBA、服务端交互式编辑。它们需要公式引擎、绘图模型或运行时解释器,与"把数据搬进文件"是两种产品。
|
|
24
|
+
|
|
25
|
+
判据很简单:需求是"生成一张给人看、或再回到 Excel 里加工的表",本库合适;需求是"在服务端读写任意 Excel 文件并保留全部特性",应选择完整实现。
|
|
26
|
+
|
|
27
|
+
## 特性
|
|
28
|
+
|
|
29
|
+
- **纯 JavaScript** - 无外部依赖(pako 打包用于 ZIP 压缩)
|
|
30
|
+
- **高性能** - 基于 BIFF12 二进制格式,文件更小、读写更快
|
|
31
|
+
- **跨平台** - 同时支持 Node.js 和浏览器环境
|
|
32
|
+
- **完整功能** - 支持读写、渲染展示
|
|
33
|
+
- **高 DPI 支持** - Canvas 渲染支持 Retina 显示屏
|
|
34
|
+
- **自动列宽** - 根据内容自动计算列宽
|
|
35
|
+
- **行列上限策略** - 16384 列 × 1048576 行,`Error` / `Cut` 可选
|
|
36
|
+
- **压缩级别可调** - ZIP 压缩 0-9,默认 6(体积与速度均衡)
|
|
37
|
+
- **ZIP64 支持** - 偏移或体积超过经典 ZIP 上限时自动切换 ZIP64
|
|
38
|
+
- **读取安全上限** - 单条目、累计解压量与条目数上限,超限抛 `ZipLimitError`
|
|
39
|
+
- **模板填充** - 基于现有 XLSB 模板填充数据,支持标记定位与流式填充
|
|
40
|
+
|
|
41
|
+
## 安装
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install jsxlsb
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
或从源码构建:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
git clone https://github.com/itcraft-cn/jsxlsb.git
|
|
51
|
+
cd jsxlsb
|
|
52
|
+
npm install
|
|
53
|
+
npm run build
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 快速开始
|
|
57
|
+
|
|
58
|
+
### Node.js - 读取
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const { XlsbReader } = require('jsxlsb');
|
|
62
|
+
|
|
63
|
+
const reader = XlsbReader.builder().path('./data.xlsb').build();
|
|
64
|
+
|
|
65
|
+
// 获取 Sheet 信息
|
|
66
|
+
const sheets = reader.getSheetInfos();
|
|
67
|
+
console.log('工作表:', sheets);
|
|
68
|
+
|
|
69
|
+
// 流式读取
|
|
70
|
+
reader.forEachRow(0, {
|
|
71
|
+
onRowStart(row, colCount) { console.log(`行 ${row} 开始`); },
|
|
72
|
+
onCell(row, col, cell) {
|
|
73
|
+
console.log(` ${row},${col}: ${cell.value}, 格式: ${cell.formatCode}`);
|
|
74
|
+
},
|
|
75
|
+
onRowEnd(row) { console.log(`行 ${row} 结束`); }
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 分页读取
|
|
79
|
+
const batch = reader.readRows(0, 100, 1000); // 从100行开始,读取1000行
|
|
80
|
+
|
|
81
|
+
reader.close();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Node.js - 写入
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
const { XlsbWriter, CellData } = require('jsxlsb');
|
|
88
|
+
|
|
89
|
+
const writer = XlsbWriter.builder().path('./output.xlsb').build();
|
|
90
|
+
|
|
91
|
+
const data = [
|
|
92
|
+
[CellData.text('姓名'), CellData.text('年龄'), CellData.text('工资')],
|
|
93
|
+
[CellData.text('张三'), CellData.number(25), CellData.currency(5000)],
|
|
94
|
+
[CellData.text('李四'), CellData.number(30), CellData.percentage(0.15)],
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
writer.writeBatch('员工表', data);
|
|
98
|
+
writer.close();
|
|
99
|
+
|
|
100
|
+
console.log('XLSB 文件创建成功: output.xlsb');
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 浏览器 - 渲染
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<script src="jsxlsb.js"></script>
|
|
107
|
+
<script>
|
|
108
|
+
const reader = await jsxlsb.XlsbReader.fromFile(file);
|
|
109
|
+
|
|
110
|
+
// HTML 表格渲染
|
|
111
|
+
const htmlRenderer = new jsxlsb.HtmlRenderer();
|
|
112
|
+
const table = htmlRenderer.render(reader, 0, {
|
|
113
|
+
maxRows: 500,
|
|
114
|
+
maxColumns: 20,
|
|
115
|
+
theme: 'light',
|
|
116
|
+
stickyHeader: true
|
|
117
|
+
});
|
|
118
|
+
document.getElementById('container').appendChild(table);
|
|
119
|
+
|
|
120
|
+
// Canvas 渲染(高 DPI + 自动列宽)
|
|
121
|
+
const canvas = document.getElementById('canvas');
|
|
122
|
+
const canvasRenderer = new jsxlsb.CanvasRenderer();
|
|
123
|
+
canvasRenderer.render(canvas, reader, 0);
|
|
124
|
+
|
|
125
|
+
reader.close();
|
|
126
|
+
</script>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## API 参考
|
|
130
|
+
|
|
131
|
+
### CellData
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
CellData.text('文本') // 文本单元格
|
|
135
|
+
CellData.number(123.45) // 数值单元格
|
|
136
|
+
CellData.number(value, formatCode) // 带格式的数值
|
|
137
|
+
CellData.date(timestamp) // 日期单元格(Excel 日期格式)
|
|
138
|
+
CellData.bool(true) // 布尔单元格
|
|
139
|
+
CellData.blank() // 空白单元格
|
|
140
|
+
|
|
141
|
+
// 内置格式
|
|
142
|
+
CellData.percentage(0.15) // 15.00%
|
|
143
|
+
CellData.percentage(0.15, 3) // 15.000%
|
|
144
|
+
CellData.numberWithComma(1234567) // 1,234,567.00
|
|
145
|
+
CellData.numberNegativeRed(-500) // -500.00(红色)
|
|
146
|
+
CellData.currency(5000) // ¥5,000.00
|
|
147
|
+
CellData.currency(5000, '$') // $5,000.00
|
|
148
|
+
CellData.time(timestamp) // h:mm:ss
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### XlsbReader
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
// Node.js
|
|
155
|
+
const reader = XlsbReader.builder().path('./data.xlsb').build();
|
|
156
|
+
const reader = XlsbReader.builder().buffer(uint8Array).build();
|
|
157
|
+
|
|
158
|
+
// 浏览器
|
|
159
|
+
const reader = await XlsbReader.fromFile(file);
|
|
160
|
+
|
|
161
|
+
// 方法
|
|
162
|
+
reader.getSheetInfos(); // 返回工作表信息数组
|
|
163
|
+
reader.getSheetDimension(index); // 只读 BrtWsDim,返回 { rowCount, columnCount }
|
|
164
|
+
reader.forEachRow(sheetIndex, handler); // 流式读取
|
|
165
|
+
reader.readRows(sheetIndex, startRow, batchSize); // 分页读取
|
|
166
|
+
reader.readRowsWithIndex(sheetIndex, startRow, batchSize); // 分页并保留真实行号
|
|
167
|
+
reader.getStyles(); // 返回 StylesReader
|
|
168
|
+
reader.close();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`readRows` 与 `readRowsWithIndex` 共用每个工作表的顺序游标与解压缓存:顺序翻页不会重复扫描已读记录,回退到更早的行时自动重置游标。
|
|
172
|
+
|
|
173
|
+
ZIP 解压安全上限(适用于不受信任的输入文件):
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
const { ZipReader } = require('jsxlsb');
|
|
177
|
+
|
|
178
|
+
const zip = new ZipReader(buffer, {
|
|
179
|
+
maxEntrySize: 64 * 1024 * 1024, // 单条目解压后上限,默认 256MB
|
|
180
|
+
maxTotalSize: 256 * 1024 * 1024, // 累计解压上限,默认 1GB
|
|
181
|
+
maxEntryCount: 10000 // 中央目录条目数上限,默认 10 万
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
- 超限抛 `ZipLimitError`,携带 `limit`(`entry-size` / `total-size` / `entry-count`)、`actual`、`max`
|
|
186
|
+
- `XlsbReader` 内部使用默认上限;需要自定义时直接用 `ZipReader` 解包后再传入 `XlsbReader.builder().buffer()`
|
|
187
|
+
|
|
188
|
+
### XlsbWriter
|
|
189
|
+
|
|
190
|
+
```javascript
|
|
191
|
+
const writer = XlsbWriter.builder().path('./output.xlsb').build();
|
|
192
|
+
const writer = XlsbWriter.builder().buffer().build(); // 返回 Uint8Array
|
|
193
|
+
|
|
194
|
+
writer.writeBatch(sheetName, data); // 写入整个工作表
|
|
195
|
+
writer.startSheet(sheetName, columnCount); // 开始流式写入
|
|
196
|
+
writer.writeRow(row); // 追加单行
|
|
197
|
+
writer.writeRows(data); // 追加多行
|
|
198
|
+
writer.writeRowsIterable(rows); // 追加任意可迭代行序列(生成器友好)
|
|
199
|
+
writer.endSheet(); // 结束工作表
|
|
200
|
+
writer.close(); // 完成并返回 Uint8Array(如果是 buffer 模式)
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
压缩级别与行列上限:
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
const writer = XlsbWriter.builder()
|
|
207
|
+
.path('./output.xlsb')
|
|
208
|
+
.compressionLevel(6) // 0=store,1-9=deflate;默认 6
|
|
209
|
+
.build();
|
|
210
|
+
|
|
211
|
+
const { MAX_COLUMNS, MAX_ROWS, OverflowBehavior, setOverflow } = require('jsxlsb');
|
|
212
|
+
setOverflow(OverflowBehavior.Cut, OverflowBehavior.Error);
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
- 上限常量:`MAX_COLUMNS = 16384`、`MAX_ROWS = 1048576`
|
|
216
|
+
- 超限行为:`Error` 抛 `LimitExceededError`;`Cut` 截断到上限
|
|
217
|
+
- 配置是全局的,每次写入操作开始时快照,只影响之后的操作
|
|
218
|
+
|
|
219
|
+
### 模板填充
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
const writer = XlsbWriter.builder()
|
|
223
|
+
.template('./template.xlsb') // 或 .templateBuffer(uint8Array)
|
|
224
|
+
.path('./output.xlsb')
|
|
225
|
+
.build();
|
|
226
|
+
|
|
227
|
+
const data = [
|
|
228
|
+
['张三', 25, 5000],
|
|
229
|
+
['李四', 30, 7500],
|
|
230
|
+
];
|
|
231
|
+
|
|
232
|
+
writer.fillBatch(0, data, 12, 8); // 指定起始行列填充
|
|
233
|
+
writer.fillAtMarker(0, '${data}', data); // 按模板中的标记定位填充
|
|
234
|
+
|
|
235
|
+
// 流式填充
|
|
236
|
+
writer.startFill(0, 12, 8);
|
|
237
|
+
writer.fillRows(data);
|
|
238
|
+
writer.endFill();
|
|
239
|
+
|
|
240
|
+
writer.close();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
- `fillBatch(sheetIndex, data, startRow, startCol)` — 从指定位置批量填充
|
|
244
|
+
- `fillAtMarker(sheetIndex, marker, data)` — 在模板标记处填充
|
|
245
|
+
- `startFill / fillRows / endFill` — 流式填充大数据集
|
|
246
|
+
- 模板模式下仅可填充模板已有工作表,`close()` 时保留模板样式与结构
|
|
247
|
+
|
|
248
|
+
### HtmlRenderer
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
const renderer = new HtmlRenderer(config);
|
|
252
|
+
|
|
253
|
+
const table = renderer.render(reader, sheetIndex, {
|
|
254
|
+
maxRows: 500,
|
|
255
|
+
maxColumns: 20,
|
|
256
|
+
theme: 'light', // 'light' 或 'dark'
|
|
257
|
+
stickyHeader: true,
|
|
258
|
+
showHeader: true,
|
|
259
|
+
cellWidth: 100
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
renderer.renderTo(container, reader, sheetIndex, config); // 直接添加到 DOM
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### CanvasRenderer
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
const renderer = new CanvasRenderer(config);
|
|
269
|
+
|
|
270
|
+
renderer.render(canvas, reader, sheetIndex, {
|
|
271
|
+
cellHeight: 24,
|
|
272
|
+
fontFamily: 'Arial',
|
|
273
|
+
fontSize: 12,
|
|
274
|
+
maxRenderRows: 50,
|
|
275
|
+
maxRenderColumns: 20
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
renderer.setZoom(1.5); // 设置缩放级别(0.5 - 3)
|
|
279
|
+
renderer.scrollTo(row, col); // 滚动到指定位置
|
|
280
|
+
renderer.resize(width, height); // 调整 Canvas 尺寸
|
|
281
|
+
renderer.destroy(); // 清理资源
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 格式化工具
|
|
285
|
+
|
|
286
|
+
```javascript
|
|
287
|
+
const { formatCell, isDateFormat, excelDateToTimestamp } = require('jsxlsb');
|
|
288
|
+
|
|
289
|
+
// 格式化单元格值
|
|
290
|
+
const result = formatCell(-500, '#,##0.00;[Red]-#,##0.00');
|
|
291
|
+
// 返回: { text: '-500.00', color: 'red' }
|
|
292
|
+
|
|
293
|
+
// 检查是否为日期/时间格式
|
|
294
|
+
isDateFormat('m/d/yy h:mm'); // true
|
|
295
|
+
|
|
296
|
+
// 转换 Excel 日期为时间戳
|
|
297
|
+
excelDateToTimestamp(46131.401); // 返回 JavaScript 时间戳
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## 格式支持
|
|
301
|
+
|
|
302
|
+
### 数字格式
|
|
303
|
+
|
|
304
|
+
| 格式代码 | 示例 |
|
|
305
|
+
|---------|------|
|
|
306
|
+
| `General` | 123.45 |
|
|
307
|
+
| `#,##0.00` | 1,234.56 |
|
|
308
|
+
| `#,##0.00;[Red]-#,##0.00` | -500.00(红色) |
|
|
309
|
+
| `¥#,##0.00` | ¥1,234.56 |
|
|
310
|
+
| `0.00%` | 15.00% |
|
|
311
|
+
|
|
312
|
+
### 日期/时间格式
|
|
313
|
+
|
|
314
|
+
| 格式代码 | 示例 |
|
|
315
|
+
|---------|------|
|
|
316
|
+
| `m/d/yy` | 4/20/26 |
|
|
317
|
+
| `m/d/yy h:mm` | 4/20/26 17:04 |
|
|
318
|
+
| `h:mm:ss` | 17:04:35 |
|
|
319
|
+
|
|
320
|
+
日期序列号以 1899-12-30 为原点,并保留 Excel 的 1900 闰年 bug:`2024-01-01` 对应 `45292`,`1900-03-01` 之前的值会差 1 天。
|
|
321
|
+
|
|
322
|
+
## 示例
|
|
323
|
+
|
|
324
|
+
参见 `examples/` 目录:
|
|
325
|
+
|
|
326
|
+
- `node-read.js` - Node.js 读取示例
|
|
327
|
+
- `node-write.js` - Node.js 写入示例
|
|
328
|
+
- `browser-canvas.html` - Canvas 渲染演示
|
|
329
|
+
- `browser-html.html` - HTML 渲染演示
|
|
330
|
+
- `browser-test.html` - 综合浏览器测试
|
|
331
|
+
|
|
332
|
+
## 构建
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
npm run build # 构建 UMD 包
|
|
336
|
+
npm run build:watch # 监听模式
|
|
337
|
+
npm run typecheck # 类型检查
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## 相关文档
|
|
341
|
+
|
|
342
|
+
- [使用手册](MANUAL.md) — 安装、构建、API 用法与示例
|
|
343
|
+
- [变更日志](CHANGELOG.md) — 版本历史
|
|
344
|
+
|
|
345
|
+
## 许可证
|
|
346
|
+
|
|
347
|
+
Apache License 2.0
|
|
348
|
+
|
|
349
|
+
## 相关项目
|
|
350
|
+
|
|
351
|
+
- [jxlsb](https://github.com/itcraft-cn/jxlsb) - 本库的 Java 版本
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { CellData } from '../core/cell/CellData';
|
|
2
|
+
import { StylesReader } from '../core/style/StylesReader';
|
|
3
|
+
import { SheetInfo, RowHandler } from './interfaces';
|
|
4
|
+
export interface XlsbReaderOptions {
|
|
5
|
+
path?: string;
|
|
6
|
+
buffer?: Uint8Array;
|
|
7
|
+
}
|
|
8
|
+
export interface SheetDimension {
|
|
9
|
+
/** 行数(last_row + 1)。 */
|
|
10
|
+
rowCount: number;
|
|
11
|
+
/** 列数(last_col + 1)。 */
|
|
12
|
+
columnCount: number;
|
|
13
|
+
}
|
|
14
|
+
export interface IndexedRow {
|
|
15
|
+
row: number;
|
|
16
|
+
cells: CellData[];
|
|
17
|
+
}
|
|
18
|
+
export declare class XlsbReader {
|
|
19
|
+
private container;
|
|
20
|
+
private sst;
|
|
21
|
+
private styles;
|
|
22
|
+
private buffer;
|
|
23
|
+
/** 已解压 sheet 缓存,避免分页/渲染重复 inflate。 */
|
|
24
|
+
private sheetBuffers;
|
|
25
|
+
/** 每张 sheet 的顺序分页游标。 */
|
|
26
|
+
private pageCursors;
|
|
27
|
+
private constructor();
|
|
28
|
+
/** 供 Builder / 外部基于已有 buffer 创建实例。 */
|
|
29
|
+
static create(buffer: Uint8Array): XlsbReader;
|
|
30
|
+
private loadSharedStringsTable;
|
|
31
|
+
private loadStylesTable;
|
|
32
|
+
getSheetInfos(): SheetInfo[];
|
|
33
|
+
forEachRow(sheetIndex: number, handler: RowHandler): void;
|
|
34
|
+
readRows(sheetIndex: number, startRow: number, batchSize: number): CellData[][];
|
|
35
|
+
/**
|
|
36
|
+
* 读取分页并保留每行的真实行号(Canvas 视口渲染使用)。
|
|
37
|
+
*
|
|
38
|
+
* 与 readRows 共享同一顺序游标:顺序调用不会重复扫描已读记录;
|
|
39
|
+
* 回退到更早的行时自动重置游标。
|
|
40
|
+
*/
|
|
41
|
+
readRowsWithIndex(sheetIndex: number, startRow: number, batchSize: number): IndexedRow[];
|
|
42
|
+
/**
|
|
43
|
+
* 读取 sheet 的维度信息(来自 BrtWsDim),不遍历全部单元格。
|
|
44
|
+
*/
|
|
45
|
+
getSheetDimension(sheetIndex: number): SheetDimension;
|
|
46
|
+
private readPage;
|
|
47
|
+
private get cellDecoder();
|
|
48
|
+
private getSheetBuffer;
|
|
49
|
+
hasSharedStrings(): boolean;
|
|
50
|
+
getStyles(): StylesReader;
|
|
51
|
+
close(): void;
|
|
52
|
+
static builder(): XlsbReaderBuilder;
|
|
53
|
+
static fromFile(file: File): Promise<XlsbReader>;
|
|
54
|
+
}
|
|
55
|
+
declare class XlsbReaderBuilder {
|
|
56
|
+
private options;
|
|
57
|
+
path(filePath: string): XlsbReaderBuilder;
|
|
58
|
+
buffer(data: Uint8Array): XlsbReaderBuilder;
|
|
59
|
+
build(): XlsbReader;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CellData } from '../core/cell/CellData';
|
|
2
|
+
export interface SheetInfo {
|
|
3
|
+
name: string;
|
|
4
|
+
index: number;
|
|
5
|
+
path: string;
|
|
6
|
+
}
|
|
7
|
+
export interface RowHandler {
|
|
8
|
+
onRowStart(rowIndex: number, columnCount: number): void;
|
|
9
|
+
onCell(row: number, col: number, cellData: CellData): void;
|
|
10
|
+
onRowEnd(rowIndex: number): void;
|
|
11
|
+
}
|
|
12
|
+
export interface SheetConsumer {
|
|
13
|
+
accept(info: SheetInfo, reader: any): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface Biff12Record {
|
|
2
|
+
type: number;
|
|
3
|
+
size: number;
|
|
4
|
+
data: Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
export declare class Biff12Reader {
|
|
7
|
+
private buffer;
|
|
8
|
+
private offset;
|
|
9
|
+
constructor(buffer: Uint8Array);
|
|
10
|
+
hasNext(): boolean;
|
|
11
|
+
nextRecord(): Biff12Record | null;
|
|
12
|
+
readAllRecords(): Biff12Record[];
|
|
13
|
+
getOffset(): number;
|
|
14
|
+
setOffset(offset: number): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function parseBrtCellRk(data: Uint8Array): {
|
|
17
|
+
col: number;
|
|
18
|
+
value: number;
|
|
19
|
+
styleIndex: number;
|
|
20
|
+
};
|
|
21
|
+
export declare function parseBrtCellReal(data: Uint8Array): {
|
|
22
|
+
col: number;
|
|
23
|
+
value: number;
|
|
24
|
+
styleIndex: number;
|
|
25
|
+
};
|
|
26
|
+
export declare function parseBrtCellBool(data: Uint8Array): {
|
|
27
|
+
col: number;
|
|
28
|
+
value: boolean;
|
|
29
|
+
styleIndex: number;
|
|
30
|
+
};
|
|
31
|
+
export declare function parseBrtCellIsst(data: Uint8Array): {
|
|
32
|
+
col: number;
|
|
33
|
+
sstIndex: number;
|
|
34
|
+
styleIndex: number;
|
|
35
|
+
};
|
|
36
|
+
export declare function parseBrtCellSt(data: Uint8Array): {
|
|
37
|
+
col: number;
|
|
38
|
+
text: string;
|
|
39
|
+
styleIndex: number;
|
|
40
|
+
};
|
|
41
|
+
export declare function parseBrtCellBlank(data: Uint8Array): {
|
|
42
|
+
col: number;
|
|
43
|
+
styleIndex: number;
|
|
44
|
+
};
|
|
45
|
+
export declare function parseBrtRowHdr(data: Uint8Array): {
|
|
46
|
+
row: number;
|
|
47
|
+
};
|
|
48
|
+
export declare function parseBrtWsDim(data: Uint8Array): {
|
|
49
|
+
rowFirst: number;
|
|
50
|
+
rowLast: number;
|
|
51
|
+
colFirst: number;
|
|
52
|
+
colLast: number;
|
|
53
|
+
};
|
|
54
|
+
export declare function parseBrtSSTItem(data: Uint8Array): string;
|