Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
corporateCulture-qd
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王立鹏
corporateCulture-qd
Commits
aca22208
Commit
aca22208
authored
Feb 13, 2026
by
lijiabin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【需求 20331】 feat: 加入前端根据数据生成excel的工具函数
parent
bb1ac253
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
index.ts
src/utils/excel/index.ts
+54
-0
No files found.
src/utils/excel/index.ts
0 → 100644
View file @
aca22208
import
*
as
XLSX
from
'xlsx'
/**
* 导出 Excel
* @param {Object[]} data 数据源
* @param {Object[]} columns 列配置
* @param {String} fileName 文件名
*/
export
interface
ExportColumn
<
T
,
K
extends
keyof
T
=
keyof
T
>
{
title
:
string
// 表头中文名
key
:
K
// 数据源中后端返回的字段名
width
?:
number
// 列宽
formatter
?:
(
value
:
T
[
K
])
=>
any
// 自定义话格式函数 比如后端返回 0 1 转换为 否 是
}
export
interface
ExportOptions
<
T
>
{
data
:
T
[]
columns
:
ExportColumn
<
T
>
[]
fileName
?:
string
sheetName
?:
string
}
export
function
exportExcel
<
T
>
({
data
,
columns
,
fileName
=
'导出数据'
,
sheetName
=
'Sheet1'
,
}:
ExportOptions
<
T
>
)
{
if
(
!
data
||
!
data
.
length
)
return
ElMessage
.
warning
(
'没有可导出的数据'
)
const
header
=
columns
.
map
((
col
)
=>
col
.
title
)
const
rows
=
data
.
map
((
row
)
=>
columns
.
map
((
col
)
=>
{
const
value
=
row
[
col
.
key
]
return
col
.
formatter
?
col
.
formatter
(
value
)
:
value
}),
)
// 生成 worksheet
const
worksheet
=
XLSX
.
utils
.
aoa_to_sheet
([
header
,
...
rows
])
// 设置列宽
worksheet
[
'!cols'
]
=
columns
.
map
((
col
)
=>
({
wch
:
col
.
width
||
15
,
}))
// 生成 workbook
const
workbook
=
XLSX
.
utils
.
book_new
()
XLSX
.
utils
.
book_append_sheet
(
workbook
,
worksheet
,
sheetName
)
// 导出
XLSX
.
writeFile
(
workbook
,
`
${
fileName
}
.xlsx`
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment