安裝
npm install xlsx --save
引用
import * as XLSX from 'xlsx/xlsx.mjs'
結(jié)合element
<el-upload
action
accept = ".xlsx, .xls"
:auto-upload="false"
:show-file-list="false"
:http-request="uploadFile">
<el-button type="primary">導(dǎo)入excel</el-button>
</el-upload>
async uploadFile(params) {
const _file = params.file
const fileReader = new FileReader()
fileReader.onload = ev => {
try {
const data = ev.target.result
console.log(data)
const workbook = XLSX.read(data, {
type: 'binary',
})
for (let sheet in workbook.Sheets) {
//循環(huán)讀取每個(gè)文件
const sheetArray = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
//若當(dāng)前sheet沒有數(shù)據(jù),則continue
if (sheetArray.length == 0) {
continue
}
console.log('讀取文件')
//解析返回的json
console.log(sheetArray)
}
} catch (e) {
this.$message.warning('文件類型不正確仰美!')
}
}
fileReader.readAsBinaryString(_file)
},