前端上传的excel文件内容

‘input type=file’ 形式

<template>
    <div>
        <input type="file" @change="importFile" id="imFile" style="display:none"
        accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
        <el-button type="primary" @click="importExcel()" size="small">导入excel</el-button>
    </div>
</template>

<script>
import XLSX from 'xlsx' 
export default {
    data(){
        return {
            importData:'',
            wb:''
        }
    },
    methods:{
        importExcel(){
            document.getElementById('imFile').click()
        },
        importFile: function(e) {
            let obj = e.target
            if(e.target.files.length==0) {
                console.log("没有获取到导入内容")
                return
            }
            var f = obj.files[0]
            var reader = new FileReader()
            reader.readAsBinaryString(f)
            reader.onload = (e) => {
                var data = e.target.result //获取上传的文件数据
                this.wb = XLSX.read(data, {
                    type: 'binary'
                })
                this.importData = XLSX.utils.sheet_to_json(this.wb.Sheets[this.wb.SheetNames[0]])
                //至此已完成对上传excel的内容解析 然后可以直接把这个对象数组传给后端
                console.log("this.importData is:",this.importData)
                // this.uploadExcelData()
            }
        },
        uploadExcelData(){
            this.imFile.value = ''
            this.$post('/recharge/import',data).then(
                res => {
                    if(res.code == 'success'){
                        this.$notify({title: '成功', message: '导入成功!', type: 'success'})
                        this.queryData()
                        this.fullscreenLoading = false
                    }else(
                        this.$alert(res.message, '导入失败', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.fullscreenLoading = false
                                this.queryData()
                            }
                        })
                    )
                }
            ).catch(err => {
                console.log("err is:",err)
                this.$notify.error({ title: '错误', message: '抱歉,导入失败!'})
                this.fullscreenLoading = false
            })
        }
    }
}
</script>

elementui 上传组件

<template>
    <div>
        <!-- <el-upload style="display: inline" action="" :show-file-list="false" :on-change="uploadExcel_change">
            <el-button size="small" type="primary">导入excel</el-button>
        </el-upload> -->
        <el-upload style="display: inline" action="" :show-file-list="false" :http-request="uploadExcel_repuest">
            <el-button size="small" type="primary">{{uploadText}}</el-button>
        </el-upload>
    </div>
</template>

<script>
import XLSX from 'xlsx'
export default {
    props:{
        uploadText: {
            type: String,
            require: true,
            default: '导入'
        },
        api: {
            type: String,
            require: true,
            default: '/'
        }
    },
    data(){
        return {
            importData: '',
            wb: ''
        }
    },
    methods: {
        uploadExcel_repuest(content){
            var reader = new FileReader()
            reader.readAsBinaryString(content.file)
            reader.onload = (e) => {
                let data = e.target.result //获取上传的文件内容
                this.wb = XLSX.read(data, {
                    type: 'binary'
                })
                this.importData = XLSX.utils.sheet_to_json(this.wb.Sheets[this.wb.SheetNames[0]])
                console.log("解析到的excel数据为:",this.importData)
            }
        },
        uploadExcel_change(content){
            var reader = new FileReader()
            reader.readAsBinaryString(content.raw)
            reader.onload = (e) => {
                let data = e.target.result //获取上传的文件内容
                this.wb = XLSX.read(data, {
                    type: 'binary'
                })
                this.importData = XLSX.utils.sheet_to_json(this.wb.Sheets[this.wb.SheetNames[0]])
                console.log("解析到的excel数据为:",this.importData)
            }
        },
        uploadExcelData(data){
            this.imFile.value = ''
            this.$post(this.api,data).then(
                res => {
                    if(res.code == 'success'){
                        this.$notify({title: '成功', message: '导入成功!', type: 'success'})
                        this.queryData()
                        this.fullscreenLoading = false
                    }else(
                        this.$alert(res.message, '导入失败', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.fullscreenLoading = false
                                this.queryData()
                            }
                        })
                    )
                }
            ).catch(err => {
                console.log("err is:",err)
                this.$notify.error({ title: '错误', message: '抱歉,导入失败!'})
                this.fullscreenLoading = false
            })
        }
    }
}
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值