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) => { 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++) { var sheetData = { 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 } return month + '/' + date
}
|
1 2 3 4 5 6 7 8 9
| const leapyear = (year) => { var flag = false; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { flag = true; } return flag;
}
|