明树Git Lab
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
J
jt_front
Project
Project
Details
Activity
Releases
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
Administrator
jt_front
Commits
33a92c02
Commit
33a92c02
authored
Jan 20, 2026
by
yangyajing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件导出
parent
8c700bf3
Pipeline
#106386
passed with stage
in 17 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
68 deletions
+95
-68
tableFileHandle.js
src/common/tableFileHandle.js
+93
-0
templateManage.vue
src/views/projectManage/templateManage.vue
+2
-68
No files found.
src/common/tableFileHandle.js
0 → 100644
View file @
33a92c02
import
*
as
XLSX
from
"xlsx"
;
// 导入excel文件显示到前端页面
const
importTableFile
=
async
(
file
)
=>
{
const
data
=
await
readFile
(
file
.
raw
);
// 使用 xlsx 解析
let
workbook
=
XLSX
.
read
(
data
,
{
type
:
'array'
,
cellDates
:
true
,
cellStyles
:
true
});
console
.
log
(
workbook
);
};
const
readFile
=
(
file
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
reader
=
new
FileReader
();
reader
.
onload
=
(
e
)
=>
resolve
(
e
.
target
.
result
);
reader
.
onerror
=
reject
;
reader
.
readAsArrayBuffer
(
file
);
});
};
// 导出页面数据,生成excel文件
const
exportTableFile
=
(
tableData
,
tableColumns
,
title
,
fileName
)
=>
{
const
wb
=
XLSX
.
utils
.
book_new
()
// 2. 准备数据
let
wsData
=
[[
title
],
[]]
// 添加表头
if
(
tableColumns
&&
tableColumns
.
length
>
0
)
{
const
headerRow
=
tableColumns
.
map
(
h
=>
h
.
label
||
h
)
wsData
.
push
(
headerRow
)
}
// 添加数据行
tableData
.
forEach
(
item
=>
{
if
(
tableColumns
)
{
// 按表头配置提取数据
const
row
=
tableColumns
.
map
(
header
=>
{
const
value
=
getNestedValue
(
item
,
header
.
prop
||
header
)
return
formatCellValue
(
value
,
header
.
formatter
)
})
wsData
.
push
(
row
)
}
else
{
// 如果没有表头配置,直接使用对象值
const
row
=
Object
.
values
(
item
)
wsData
.
push
(
row
)
}
})
// 3. 创建工作表
const
ws
=
XLSX
.
utils
.
aoa_to_sheet
(
wsData
)
// 4. 设置列宽(可选)
if
(
tableColumns
)
{
const
colWidths
=
tableColumns
.
map
(
header
=>
({
wch
:
header
.
width
||
Math
.
max
(
(
header
.
label
||
header
).
length
,
...
tableData
.
map
(
item
=>
{
const
value
=
getNestedValue
(
item
,
header
.
prop
||
header
)
return
String
(
value
||
''
).
length
})
)
*
2
}))
ws
[
'!cols'
]
=
colWidths
}
// 合并标题单元格
ws
[
'!merges'
]
=
[
XLSX
.
utils
.
decode_range
(
'A1:Q1'
)
// 合并第一行的 A-E 列
]
// 5. 将工作表添加到工作簿
XLSX
.
utils
.
book_append_sheet
(
wb
,
ws
,
'Sheet1'
)
// 6. 生成 Excel 文件并下载
XLSX
.
writeFile
(
wb
,
fileName
+
".xlsx"
);
};
// 辅助函数:获取嵌套对象的值
const
getNestedValue
=
(
obj
,
path
)
=>
{
return
path
.
split
(
'.'
).
reduce
((
prev
,
curr
)
=>
{
return
prev
?
prev
[
curr
]
:
null
},
obj
)
}
// 辅助函数:格式化单元格值
const
formatCellValue
=
(
value
,
formatter
)
=>
{
if
(
formatter
&&
typeof
formatter
===
'function'
)
{
return
formatter
(
value
)
}
return
value
}
export
{
importTableFile
,
exportTableFile
}
\ No newline at end of file
src/views/projectManage/templateManage.vue
View file @
33a92c02
...
...
@@ -31,7 +31,7 @@
<
script
setup
>
import
{
reactive
,
ref
}
from
"vue"
;
import
*
as
XLSX
from
"xlsx
"
;
import
{
exportTableFile
}
from
"../../common/tableFileHandle
"
;
const
tableColumns
=
reactive
([
{
prop
:
"projectName"
,
...
...
@@ -116,74 +116,8 @@
}).
catch
(()
=>
{});
};
const
exportData
=
()
=>
{
// 1. 创建工作簿
const
wb
=
XLSX
.
utils
.
book_new
()
// 2. 准备数据
let
wsData
=
[[
"投资项目批复意见落实情况统计表"
],
[]]
// 添加表头
if
(
tableColumns
&&
tableColumns
.
length
>
0
)
{
const
headerRow
=
tableColumns
.
map
(
h
=>
h
.
label
||
h
)
wsData
.
push
(
headerRow
)
}
// 添加数据行
tableData
.
value
.
forEach
(
item
=>
{
if
(
tableColumns
)
{
// 按表头配置提取数据
const
row
=
tableColumns
.
map
(
header
=>
{
const
value
=
getNestedValue
(
item
,
header
.
prop
||
header
)
return
formatCellValue
(
value
,
header
.
formatter
)
})
wsData
.
push
(
row
)
}
else
{
// 如果没有表头配置,直接使用对象值
const
row
=
Object
.
values
(
item
)
wsData
.
push
(
row
)
}
})
// 3. 创建工作表
const
ws
=
XLSX
.
utils
.
aoa_to_sheet
(
wsData
)
// 4. 设置列宽(可选)
if
(
tableColumns
)
{
const
colWidths
=
tableColumns
.
map
(
header
=>
({
wch
:
header
.
width
||
Math
.
max
(
(
header
.
label
||
header
).
length
,
...
tableData
.
value
.
map
(
item
=>
{
const
value
=
getNestedValue
(
item
,
header
.
prop
||
header
)
return
String
(
value
||
''
).
length
})
)
*
2
}))
ws
[
'!cols'
]
=
colWidths
}
// 合并标题单元格
ws
[
'!merges'
]
=
[
XLSX
.
utils
.
decode_range
(
'A1:Q1'
)
// 合并第一行的 A-E 列
]
// 5. 将工作表添加到工作簿
XLSX
.
utils
.
book_append_sheet
(
wb
,
ws
,
'Sheet1'
)
// 6. 生成 Excel 文件并下载
XLSX
.
writeFile
(
wb
,
"导出文件.xlsx"
);
exportTableFile
(
tableData
.
value
,
tableColumns
,
"投资项目批复意见落实情况统计表"
,
"导出文件"
)
};
// 辅助函数:获取嵌套对象的值
function
getNestedValue
(
obj
,
path
)
{
return
path
.
split
(
'.'
).
reduce
((
prev
,
curr
)
=>
{
return
prev
?
prev
[
curr
]
:
null
},
obj
)
}
// 辅助函数:格式化单元格值
function
formatCellValue
(
value
,
formatter
)
{
if
(
formatter
&&
typeof
formatter
===
'function'
)
{
return
formatter
(
value
)
}
return
value
}
</
script
>
<
style
lang=
"less"
>
...
...
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