react-util-tools 1.0.24 → 1.0.26
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/README.md +128 -3
- package/dist/index.cjs +437 -2
- package/dist/index.d.cts +68 -1
- package/dist/index.d.ts +68 -1
- package/dist/index.js +373 -1
- package/package.json +8 -3
- package/src/excel/README.md +388 -0
- package/src/excel/example.md +425 -0
- package/src/excel/index.ts +245 -0
- package/src/index.ts +78 -0
- package/src/string/README.md +441 -0
- package/src/string/index.ts +527 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
# Excel - SheetJS 封装
|
|
2
|
+
|
|
3
|
+
基于 [SheetJS (xlsx)](https://docs.sheetjs.com/) 的 Excel 文件处理工具,保留了 SheetJS 的所有原生功能。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ 完整导出 SheetJS 所有功能
|
|
8
|
+
- 📖 读取 Excel 文件(.xlsx, .xls, .csv 等)
|
|
9
|
+
- 📝 创建和导出 Excel 文件
|
|
10
|
+
- 🔄 JSON 与 Excel 互转
|
|
11
|
+
- 🌐 支持多种格式(CSV, HTML, AOA 等)
|
|
12
|
+
- 💻 浏览器和 Node.js 环境均可使用
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install react-util-tools
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 基础用法
|
|
21
|
+
|
|
22
|
+
### 导入模块
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import {
|
|
26
|
+
XLSX, // 完整的 XLSX 对象
|
|
27
|
+
readExcelFile, // 读取 Excel 文件
|
|
28
|
+
exportJSONToExcel, // 导出 JSON 为 Excel
|
|
29
|
+
workbookToJSON, // WorkBook 转 JSON
|
|
30
|
+
jsonToWorkbook, // JSON 转 WorkBook
|
|
31
|
+
utils // XLSX 工具函数
|
|
32
|
+
} from 'react-util-tools'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## 常用功能
|
|
36
|
+
|
|
37
|
+
### 1. 读取 Excel 文件
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { readExcelFile, workbookToJSON } from 'react-util-tools'
|
|
41
|
+
|
|
42
|
+
// 从 File 对象读取
|
|
43
|
+
async function handleFileUpload(file: File) {
|
|
44
|
+
try {
|
|
45
|
+
const workbook = await readExcelFile(file)
|
|
46
|
+
|
|
47
|
+
// 获取所有工作表名称
|
|
48
|
+
console.log(workbook.SheetNames) // ['Sheet1', 'Sheet2']
|
|
49
|
+
|
|
50
|
+
// 转换为 JSON
|
|
51
|
+
const data = workbookToJSON(workbook)
|
|
52
|
+
console.log(data)
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error('读取失败:', error)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 在 React 中使用
|
|
59
|
+
function ExcelUploader() {
|
|
60
|
+
const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
61
|
+
const file = e.target.files?.[0]
|
|
62
|
+
if (file) {
|
|
63
|
+
await handleFileUpload(file)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return <input type="file" accept=".xlsx,.xls" onChange={handleChange} />
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 2. 直接读取为 JSON
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { readExcelToJSON } from 'react-util-tools'
|
|
75
|
+
|
|
76
|
+
async function loadExcelData(file: File) {
|
|
77
|
+
// 读取第一个工作表
|
|
78
|
+
const data = await readExcelToJSON(file)
|
|
79
|
+
console.log(data)
|
|
80
|
+
|
|
81
|
+
// 读取指定工作表
|
|
82
|
+
const sheet2Data = await readExcelToJSON(file, 'Sheet2')
|
|
83
|
+
console.log(sheet2Data)
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. 导出 JSON 为 Excel
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { exportJSONToExcel } from 'react-util-tools'
|
|
91
|
+
|
|
92
|
+
function exportData() {
|
|
93
|
+
const data = [
|
|
94
|
+
{ name: '张三', age: 25, city: '北京' },
|
|
95
|
+
{ name: '李四', age: 30, city: '上海' },
|
|
96
|
+
{ name: '王五', age: 28, city: '广州' }
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
// 导出为 Excel 文件
|
|
100
|
+
exportJSONToExcel(data, 'users.xlsx', 'UserList')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 在 React 中使用
|
|
104
|
+
function ExportButton() {
|
|
105
|
+
return <button onClick={exportData}>导出 Excel</button>
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 4. 创建多工作表 Excel
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { XLSX, utils, exportExcelFile } from 'react-util-tools'
|
|
113
|
+
|
|
114
|
+
function exportMultiSheet() {
|
|
115
|
+
// 创建工作簿
|
|
116
|
+
const workbook = utils.book_new()
|
|
117
|
+
|
|
118
|
+
// 第一个工作表
|
|
119
|
+
const users = [
|
|
120
|
+
{ name: '张三', age: 25 },
|
|
121
|
+
{ name: '李四', age: 30 }
|
|
122
|
+
]
|
|
123
|
+
const sheet1 = utils.json_to_sheet(users)
|
|
124
|
+
utils.book_append_sheet(workbook, sheet1, '用户列表')
|
|
125
|
+
|
|
126
|
+
// 第二个工作表
|
|
127
|
+
const products = [
|
|
128
|
+
{ product: 'iPhone', price: 5999 },
|
|
129
|
+
{ product: 'iPad', price: 3999 }
|
|
130
|
+
]
|
|
131
|
+
const sheet2 = utils.json_to_sheet(products)
|
|
132
|
+
utils.book_append_sheet(workbook, sheet2, '产品列表')
|
|
133
|
+
|
|
134
|
+
// 导出文件
|
|
135
|
+
exportExcelFile(workbook, 'data.xlsx')
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### 5. 自定义表头
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { jsonToWorkbook, exportExcelFile } from 'react-util-tools'
|
|
143
|
+
|
|
144
|
+
function exportWithCustomHeaders() {
|
|
145
|
+
const data = [
|
|
146
|
+
{ name: '张三', age: 25, city: '北京' },
|
|
147
|
+
{ name: '李四', age: 30, city: '上海' }
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
// 自定义表头
|
|
151
|
+
const workbook = jsonToWorkbook(data, 'Sheet1', {
|
|
152
|
+
header: ['姓名', '年龄', '城市']
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
exportExcelFile(workbook, 'custom-headers.xlsx')
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 6. 使用 AOA (Array of Arrays)
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { aoaToSheet, utils, exportExcelFile } from 'react-util-tools'
|
|
163
|
+
|
|
164
|
+
function exportFromArray() {
|
|
165
|
+
const data = [
|
|
166
|
+
['姓名', '年龄', '城市'],
|
|
167
|
+
['张三', 25, '北京'],
|
|
168
|
+
['李四', 30, '上海'],
|
|
169
|
+
['王五', 28, '广州']
|
|
170
|
+
]
|
|
171
|
+
|
|
172
|
+
const worksheet = aoaToSheet(data)
|
|
173
|
+
const workbook = utils.book_new()
|
|
174
|
+
utils.book_append_sheet(workbook, worksheet, 'Sheet1')
|
|
175
|
+
|
|
176
|
+
exportExcelFile(workbook, 'array-data.xlsx')
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 7. 转换为 CSV
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
import { readExcelFile, getSheet, sheetToCSV } from 'react-util-tools'
|
|
184
|
+
|
|
185
|
+
async function convertToCSV(file: File) {
|
|
186
|
+
const workbook = await readExcelFile(file)
|
|
187
|
+
const sheet = getSheet(workbook, 'Sheet1')
|
|
188
|
+
const csv = sheetToCSV(sheet)
|
|
189
|
+
|
|
190
|
+
console.log(csv)
|
|
191
|
+
|
|
192
|
+
// 下载 CSV
|
|
193
|
+
const blob = new Blob([csv], { type: 'text/csv' })
|
|
194
|
+
const url = URL.createObjectURL(blob)
|
|
195
|
+
const a = document.createElement('a')
|
|
196
|
+
a.href = url
|
|
197
|
+
a.download = 'data.csv'
|
|
198
|
+
a.click()
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 8. 从 HTML 表格创建 Excel
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { tableToSheet, utils, exportExcelFile } from 'react-util-tools'
|
|
206
|
+
|
|
207
|
+
function exportTableToExcel() {
|
|
208
|
+
const table = document.querySelector('table')
|
|
209
|
+
if (!table) return
|
|
210
|
+
|
|
211
|
+
const worksheet = tableToSheet(table)
|
|
212
|
+
const workbook = utils.book_new()
|
|
213
|
+
utils.book_append_sheet(workbook, worksheet, 'Sheet1')
|
|
214
|
+
|
|
215
|
+
exportExcelFile(workbook, 'table-export.xlsx')
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## 高级用法
|
|
220
|
+
|
|
221
|
+
### 使用完整的 XLSX 对象
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
import { XLSX } from 'react-util-tools'
|
|
225
|
+
|
|
226
|
+
// 访问所有 SheetJS 功能
|
|
227
|
+
const workbook = XLSX.read(data, { type: 'binary' })
|
|
228
|
+
const worksheet = workbook.Sheets[workbook.SheetNames[0]]
|
|
229
|
+
|
|
230
|
+
// 设置单元格样式(需要 xlsx-style 或 xlsx-js-style)
|
|
231
|
+
worksheet['A1'].s = {
|
|
232
|
+
font: { bold: true },
|
|
233
|
+
fill: { fgColor: { rgb: 'FFFF00' } }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 合并单元格
|
|
237
|
+
worksheet['!merges'] = [
|
|
238
|
+
{ s: { r: 0, c: 0 }, e: { r: 0, c: 2 } }
|
|
239
|
+
]
|
|
240
|
+
|
|
241
|
+
// 设置列宽
|
|
242
|
+
worksheet['!cols'] = [
|
|
243
|
+
{ wch: 20 },
|
|
244
|
+
{ wch: 15 },
|
|
245
|
+
{ wch: 30 }
|
|
246
|
+
]
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### 读取选项
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { readExcelFile } from 'react-util-tools'
|
|
253
|
+
|
|
254
|
+
const workbook = await readExcelFile(file, {
|
|
255
|
+
type: 'binary', // 数据类型
|
|
256
|
+
cellDates: true, // 将日期解析为 Date 对象
|
|
257
|
+
cellNF: false, // 不保存数字格式
|
|
258
|
+
cellStyles: false, // 不保存样式
|
|
259
|
+
sheetStubs: false, // 不生成空单元格
|
|
260
|
+
raw: false // 不使用原始值
|
|
261
|
+
})
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### 写入选项
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import { exportExcelFile } from 'react-util-tools'
|
|
268
|
+
|
|
269
|
+
exportExcelFile(workbook, 'output.xlsx', {
|
|
270
|
+
bookType: 'xlsx', // 文件类型
|
|
271
|
+
type: 'binary', // 输出类型
|
|
272
|
+
compression: true, // 启用压缩
|
|
273
|
+
Props: { // 文档属性
|
|
274
|
+
Title: '数据报表',
|
|
275
|
+
Author: '系统管理员',
|
|
276
|
+
CreatedDate: new Date()
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## 支持的文件格式
|
|
282
|
+
|
|
283
|
+
### 读取格式
|
|
284
|
+
- `.xlsx` - Excel 2007+ XML 格式
|
|
285
|
+
- `.xlsb` - Excel 2007+ 二进制格式
|
|
286
|
+
- `.xlsm` - Excel 2007+ 宏文件
|
|
287
|
+
- `.xls` - Excel 97-2004 格式
|
|
288
|
+
- `.csv` - 逗号分隔值
|
|
289
|
+
- `.txt` - 文本文件
|
|
290
|
+
- `.ods` - OpenDocument 电子表格
|
|
291
|
+
- 更多格式请参考 [SheetJS 文档](https://docs.sheetjs.com/)
|
|
292
|
+
|
|
293
|
+
### 导出格式
|
|
294
|
+
- `xlsx` - Excel 2007+ XML 格式(默认)
|
|
295
|
+
- `xlsb` - Excel 2007+ 二进制格式
|
|
296
|
+
- `xls` - Excel 97-2004 格式
|
|
297
|
+
- `csv` - 逗号分隔值
|
|
298
|
+
- `txt` - UTF-16 文本
|
|
299
|
+
- `html` - HTML 表格
|
|
300
|
+
- `ods` - OpenDocument 电子表格
|
|
301
|
+
|
|
302
|
+
## API 参考
|
|
303
|
+
|
|
304
|
+
### 核心导出
|
|
305
|
+
|
|
306
|
+
- `XLSX` - 完整的 SheetJS 对象
|
|
307
|
+
- `utils` - SheetJS 工具函数集合
|
|
308
|
+
- `read` - 读取数据
|
|
309
|
+
- `write` - 写入数据
|
|
310
|
+
- `readFile` - 读取文件(Node.js)
|
|
311
|
+
- `writeFile` - 写入文件
|
|
312
|
+
|
|
313
|
+
### 封装函数
|
|
314
|
+
|
|
315
|
+
#### readExcelFile(file, options?)
|
|
316
|
+
从 File 对象读取 Excel 文件
|
|
317
|
+
|
|
318
|
+
#### workbookToJSON(workbook, sheetName?, options?)
|
|
319
|
+
将 WorkBook 转换为 JSON 数组
|
|
320
|
+
|
|
321
|
+
#### jsonToWorkbook(data, sheetName?, options?)
|
|
322
|
+
从 JSON 数组创建 WorkBook
|
|
323
|
+
|
|
324
|
+
#### exportExcelFile(workbook, filename?, options?)
|
|
325
|
+
导出 Excel 文件(浏览器下载)
|
|
326
|
+
|
|
327
|
+
#### exportJSONToExcel(data, filename?, sheetName?, options?)
|
|
328
|
+
直接从 JSON 导出 Excel 文件
|
|
329
|
+
|
|
330
|
+
#### readExcelToJSON(file, sheetName?, parseOptions?, jsonOptions?)
|
|
331
|
+
读取 Excel 文件并转换为 JSON
|
|
332
|
+
|
|
333
|
+
#### getSheetNames(workbook)
|
|
334
|
+
获取所有工作表名称
|
|
335
|
+
|
|
336
|
+
#### getSheet(workbook, sheetName)
|
|
337
|
+
获取指定工作表
|
|
338
|
+
|
|
339
|
+
#### sheetToCSV(worksheet, options?)
|
|
340
|
+
将工作表转换为 CSV 字符串
|
|
341
|
+
|
|
342
|
+
#### sheetToHTML(worksheet, options?)
|
|
343
|
+
将工作表转换为 HTML 字符串
|
|
344
|
+
|
|
345
|
+
#### tableToSheet(table, options?)
|
|
346
|
+
从 HTML 表格创建工作表
|
|
347
|
+
|
|
348
|
+
#### aoaToSheet(data, options?)
|
|
349
|
+
从二维数组创建工作表
|
|
350
|
+
|
|
351
|
+
#### sheetToAOA(worksheet, options?)
|
|
352
|
+
将工作表转换为二维数组
|
|
353
|
+
|
|
354
|
+
## TypeScript 支持
|
|
355
|
+
|
|
356
|
+
完整的 TypeScript 类型定义:
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
import type {
|
|
360
|
+
WorkBook,
|
|
361
|
+
WorkSheet,
|
|
362
|
+
CellObject,
|
|
363
|
+
Range,
|
|
364
|
+
WritingOptions,
|
|
365
|
+
ParsingOptions,
|
|
366
|
+
BookType,
|
|
367
|
+
Sheet2JSONOpts,
|
|
368
|
+
JSON2SheetOpts
|
|
369
|
+
} from 'react-util-tools'
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
## 注意事项
|
|
373
|
+
|
|
374
|
+
1. **浏览器环境**:文件读取使用 FileReader API
|
|
375
|
+
2. **文件大小**:处理大文件时注意内存占用
|
|
376
|
+
3. **样式支持**:基础版本不支持样式,需要额外的库
|
|
377
|
+
4. **日期格式**:Excel 日期需要特殊处理
|
|
378
|
+
5. **公式**:默认读取公式的计算结果,不是公式本身
|
|
379
|
+
|
|
380
|
+
## 相关链接
|
|
381
|
+
|
|
382
|
+
- [SheetJS 官方文档](https://docs.sheetjs.com/)
|
|
383
|
+
- [SheetJS GitHub](https://github.com/SheetJS/sheetjs)
|
|
384
|
+
- [在线演示](https://sheetjs.com/demos)
|
|
385
|
+
|
|
386
|
+
## 示例项目
|
|
387
|
+
|
|
388
|
+
完整的使用示例请参考项目中的示例代码。
|