<a-upload accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
:beforeUpload="beforeUpload" @change="handleFileChange">
<a-button type="primary">导入Excel</a-button>
</a-upload>
methods:
//导入excel
beforeUpload(file){
let _this = this
return new Promise(function(resolve, reject){
// readExcel方法也使用了Promise异步转同步,此处使用then对返回值进行处理
_this.readExcel(file).then(result => {
// 此时标识校验成功,为resolve返回
if (result) resolve(result)
})
})
},
//解析Excel
readExcel(file) {
let _this = this
return new Promise(function (resolve, reject) {// 返回Promise对象
const reader = new FileReader()
reader.onload = (e) => {// 异步执行
try {
// 以二进制流方式读取得到整份excel表格对象
let data = e.target.result, workbook = XLSX.read(data, {type: 'binary'})
const exlname = workbook.SheetNames[0] // 取第一张表
const exl = XLSX.utils.sheet_to_json(workbook.Sheets[exlname]) // 生成json表格内容
console.log(exl)
let arr = []
exl.map((v, i) => {
let obj = {}
//对获取的Excel数据进行操作
arr.push(obj)
})
_this.dataSource = arr
resolve()
} catch (e) {
reject(e.message)
}
}
reader.readAsBinaryString(file)
})
},
vue实现Excel导入
https://blog.csdn.net/sinat_35538827/article/details/101444051
https://blog.csdn.net/qq_41348029/article/details/84584276
文件类型:
https://blog.csdn.net/usuallyuser/article/details/83060341
Excel限制文本长度
http://www.veryhuo.com/a/view/80048.html
Excel设置下拉选项
https://baijiahao.baidu.com/s?id=1599864933052928528&wfr=spider&for=pc