1
2
3
4
<el-upload action accept=".xlsx, .xls" :auto-upload="false" :show-file-list="false" :on-change="handle">
<el-button type="primary" slot="trigger" :loading="loading">{{loading==true?'读取中':'选取EXCEL文件'}}
</el-button>
</el-upload>

出现的问题:日期(月/日)格式的字符变成了一串数字400356这种的
最开始excel格式全是字符 后来换了个user 需求(bug)来了

>import * as xlsx from 'xlsx'
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
const handle = async (file) => {
//读取FILE中的数据(变为JSON格式)
loading.value = true
let data = await readFile(file.raw);
let workbook = xlsx.read(data, {
type: "binary"
}),
worksheet = workbook.Sheets[workbook.SheetNames[0]];
let dataList = xlsx.utils.sheet_to_json(worksheet);
// 打印结果加下图
tempData.length = 0;
for (var i = 0; i < dataList.length; i++) {
// console.log("------------" + JSON.stringify(dataList[i]));
var sheetData = {
// 键名为绑定 el 表格的关键字,值则是 dataList[i][对应表头名]
date: formatDate(dataList[i]["异常日期"], "/"),
name: dataList[i]["姓名"],
jobNumber: dataList[i]["工号"],
excep: dataList[i]["异常"],
};
console.log("------------" + JSON.stringify(sheetData));
tempData.push(sheetData);
}
if (tempData.length > 0) {
count.value = tempData.length
show.value = true;
loading.value = false;
}
}
1
2
3
4
5
6
7
8
9
const readFile = (file) => {
return new Promise(resolve => {
let reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = ev => {
resolve(ev.target.result);
};
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const formatDate = (numb, format) => {
console.log(numb + '---------' + format);
if (numb.toString().indexOf('/') != -1) return numb
const time = new Date((numb - 1) * 24 * 3600000 + 1)
time.setYear(time.getFullYear() - 70)
const year = time.getFullYear() + ''
const month = time.getMonth() + 1 + ''
const date = time.getDate() + ''
if (leapyear(year)) {
date = time.getDate() - 1 + ''
}
if (format && format.length === 1) {
return month + format + date
}
// (month < 10 ? '0' + month : month)+(date < 10 ? '0' + date : date)
return month + '/' + date //需要的格式

}
1
2
3
4
5
6
7
8
9
//区分闰年与平年   最开始没区分  出现了4/0这种情况  日期少了一天
const leapyear = (year) => {
var flag = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = true;
}
return flag;

}