Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-vue-admin
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-vue-admin
Commits
6e0c70f4
提交
6e0c70f4
authored
9月 21, 2021
作者:
无木
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(import-excel): support datetime raw data import
importExcel支持导入原始日期时间数据 fixed: #1215
上级
01b667fa
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
4 行删除
+35
-4
ImportExcel.vue
src/components/Excel/src/ImportExcel.vue
+34
-3
ImportExcel.vue
src/views/demo/excel/ImportExcel.vue
+1
-1
没有找到文件。
src/components/Excel/src/ImportExcel.vue
浏览文件 @
6e0c70f4
...
@@ -15,12 +15,25 @@
...
@@ -15,12 +15,25 @@
<
script
lang=
"ts"
>
<
script
lang=
"ts"
>
import
{
defineComponent
,
ref
,
unref
}
from
'vue'
;
import
{
defineComponent
,
ref
,
unref
}
from
'vue'
;
import
XLSX
from
'xlsx'
;
import
XLSX
from
'xlsx'
;
import
{
dateUtil
}
from
'/@/utils/dateUtil'
;
import
type
{
ExcelData
}
from
'./typing'
;
import
type
{
ExcelData
}
from
'./typing'
;
export
default
defineComponent
({
export
default
defineComponent
({
name
:
'ImportExcel'
,
name
:
'ImportExcel'
,
props
:
{
// 日期时间格式。如果不提供或者提供空值,将返回原始Date对象
dateFormat
:
{
type
:
String
,
},
// 时区调整。实验性功能,仅为了解决读取日期时间值有偏差的问题。目前仅提供了+08:00时区的偏差修正值
// https://github.com/SheetJS/sheetjs/issues/1470#issuecomment-501108554
timeZone
:
{
type
:
Number
,
default
:
8
,
},
},
emits
:
[
'success'
,
'error'
],
emits
:
[
'success'
,
'error'
],
setup
(
_
,
{
emit
})
{
setup
(
props
,
{
emit
})
{
const
inputRef
=
ref
<
HTMLInputElement
|
null
>
(
null
);
const
inputRef
=
ref
<
HTMLInputElement
|
null
>
(
null
);
const
loadingRef
=
ref
<
Boolean
>
(
false
);
const
loadingRef
=
ref
<
Boolean
>
(
false
);
...
@@ -51,10 +64,28 @@
...
@@ -51,10 +64,28 @@
*/
*/
function
getExcelData
(
workbook
:
XLSX
.
WorkBook
)
{
function
getExcelData
(
workbook
:
XLSX
.
WorkBook
)
{
const
excelData
:
ExcelData
[]
=
[];
const
excelData
:
ExcelData
[]
=
[];
const
{
dateFormat
,
timeZone
}
=
props
;
for
(
const
sheetName
of
workbook
.
SheetNames
)
{
for
(
const
sheetName
of
workbook
.
SheetNames
)
{
const
worksheet
=
workbook
.
Sheets
[
sheetName
];
const
worksheet
=
workbook
.
Sheets
[
sheetName
];
const
header
:
string
[]
=
getHeaderRow
(
worksheet
);
const
header
:
string
[]
=
getHeaderRow
(
worksheet
);
const
results
=
XLSX
.
utils
.
sheet_to_json
(
worksheet
);
let
results
=
XLSX
.
utils
.
sheet_to_json
(
worksheet
,
{
raw
:
true
,
dateNF
:
dateFormat
,
//Not worked
})
as
object
[];
results
=
results
.
map
((
row
:
object
)
=>
{
for
(
let
field
in
row
)
{
if
(
row
[
field
]
instanceof
Date
)
{
if
(
timeZone
===
8
)
{
row
[
field
].
setSeconds
(
row
[
field
].
getSeconds
()
+
43
);
}
if
(
dateFormat
)
{
row
[
field
]
=
dateUtil
(
row
[
field
]).
format
(
dateFormat
);
}
}
}
return
row
;
});
excelData
.
push
({
excelData
.
push
({
header
,
header
,
results
,
results
,
...
@@ -76,7 +107,7 @@
...
@@ -76,7 +107,7 @@
reader
.
onload
=
async
(
e
)
=>
{
reader
.
onload
=
async
(
e
)
=>
{
try
{
try
{
const
data
=
e
.
target
&&
e
.
target
.
result
;
const
data
=
e
.
target
&&
e
.
target
.
result
;
const
workbook
=
XLSX
.
read
(
data
,
{
type
:
'array'
});
const
workbook
=
XLSX
.
read
(
data
,
{
type
:
'array'
,
cellDates
:
true
});
// console.log(workbook);
// console.log(workbook);
/* DO SOMETHING WITH workbook HERE */
/* DO SOMETHING WITH workbook HERE */
const
excelData
=
getExcelData
(
workbook
);
const
excelData
=
getExcelData
(
workbook
);
...
...
src/views/demo/excel/ImportExcel.vue
浏览文件 @
6e0c70f4
<
template
>
<
template
>
<PageWrapper
title=
"excel数据导入示例"
>
<PageWrapper
title=
"excel数据导入示例"
>
<ImpExcel
@
success=
"loadDataSuccess"
>
<ImpExcel
@
success=
"loadDataSuccess"
dateFormat=
"YYYY-MM-DD"
>
<a-button
class=
"m-3"
>
导入Excel
</a-button>
<a-button
class=
"m-3"
>
导入Excel
</a-button>
</ImpExcel>
</ImpExcel>
<BasicTable
<BasicTable
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论