-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-xlsx.ts
82 lines (72 loc) · 2.11 KB
/
export-xlsx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 基于 https://github.com/SheetJS/sheetjs
type ColumnsType = {
title: string
render: string | ((row: any) => any)
numberFormat?: string // 设置xlsx的数字格式, 比如$#,##0.00
wpx?: number // 根据px设置列宽度
wch?: number // 根据字符数设置列宽度
hidden?: boolean
}[]
export const exportXLSX = (
data: any[],
columns: ColumnsType,
sheetName: string,
fileName?: string
) => {
const innerTransForm = (value: any) => {
if (value === null || value === undefined) {
return ''
}
return value
}
columns = columns.filter(column => !column.hidden)
const header = columns.map(column => column.title)
const body = data.map(obj => {
return columns.map(column => {
const { render } = column
let value
if (typeof render === 'function') {
value = render(obj)
} else if (typeof render === 'string') {
value = obj[render]
}
value = innerTransForm(value)
return value
})
})
const { XLSX } = window // XLSX library
const worksheet = XLSX.utils.aoa_to_sheet([header, ...body])
worksheet['!cols'] = columns.map(column => {
const { wpx, wch } = column
if (wch) {
return { wch }
}
if (wpx) {
return { wpx }
}
return {}
})
type NumberFormatColumns = { columnIndex: number; format: string }[]
const numberFormatColumns = columns.reduce((pre, curColumn, columnIndex) => {
return curColumn.numberFormat
? [...pre, { columnIndex, format: curColumn.numberFormat }]
: pre
}, [] as NumberFormatColumns)
// * cellKey: !ref or A2, B2 ...
// * cellObj: 'A1:C4' or { t: 's', v: 'value'}
Object.keys(worksheet).forEach(cellKey => {
const cellObj = worksheet[cellKey]
if (typeof cellObj !== 'object') {
return
}
const cell = XLSX.utils.decode_cell(cellKey)
numberFormatColumns.forEach(nfc => {
if (nfc.columnIndex === cell.c) {
cellObj.z = nfc.format
}
})
})
const workbook = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName)
XLSX.writeFile(workbook, `${fileName || sheetName}.xlsx`)
}