通过输入框导入excel表格并解析内容
前提安装
npm i xlsx
html代码
<label for="excel">导入excel</label>
<input type="file" hidden id="excel" :accept="['.xml','.xls','.xlsx']" @change="handleChange($event)">
导入表格并且解析: js代码块
handleChange(e){
this.file = e.target.files[0];
let reader = new FileReader()
let _this = this
reader.readAsArrayBuffer(this.file)
reader.onload = function () {
let buffer = reader.result
let bytes = new Uint8Array(buffer)
let length = bytes.byteLength
let binary = ''
for (let i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i])
}
let XLSX = require('xlsx')
let wb = XLSX.read(binary, {
type: 'binary'
})
let outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
console.log(outdata)
}
}
|