vue--excel 가져오기 내보내기 및 로컬 템플릿 다운로드

8073 단어 vue

                
        
            
        

beforeUpload(file) {
            const Xls = file.name.split('.');
            const islt20 = file.size / 1024 / 1024 < 20;
            if (Xls[1] == 'xls' || Xls[1] == 'xlsx') {
            } else {
                this.$message.warning('    xls  xlsx     ');
                return;
            }
            if (!islt20) {
                this.$message.warning('        20M');
                return;
            }
            this.file = file;
            return true;
        },
        async uploadRequest() {
            let formData = new FormData();
            formData.append('file', this.file);
            let res = await request({
                baseURL: '',
                url: '',
                methods: 'post',
                data: formData,
                onUploadProgress(event){
                    if(event.lengthComputable){
                        this.percentage = Math.floor(event.loaded/event.total * 100)
                    }
                }
            });
            if (res.resultCode == '00') {
                this.$message.success(res.resultData.message);
            } else {
                this.$message.success(res.resultData.message);
            }
        },
        submitUpload(){
            this.$refs.upload.submit()
        },

 
업로드 및 다운로드



import request from '@utils/requset';
export default {
    data() {
        return {
            file: [],
            fileList: [],
            isConfirm:true,
        };
    },
    methods: {
//       ,       :auto-upload="false" :on-change="handleChange"
        beforeUpload(file) {
            const Xls = file.name.split('.');
            const islt20 = file.size / 1024 / 1024 < 20;
            if (Xls[1] == 'xls' || Xls[1] == 'xlsx') {
            } else {v
                this.$message.warning('    xls  xlsx     ');
                return;
            }
            if (!islt20) {
                this.$message.warning('        20M');
                return;
            }
            this.file = file;
            return true;
        },
        async handleChange() {
            if (!this.isConfirm) {
                this.isConfirm = true;
                return;
            }
            const confirmText = ['1', '2', '3'];
            const newDatas = [];
            const h = this.$createElement;
            for (const i in confirmText) {
                newDatas.push(h('p', null, confirmText[i]));
            }
            const bo = await this.$confirm(h('div', null, newDatas), '  ', {
                confirmButtonText: '  ',
                cancelButtonText: '  ',
                type: 'warning'
            })
                .then(() => {
                    return false;
                })
                .catch(() => {
                    return true;
                });

            if (bo) {
                this.$refs.upload.submit();
                this.isConfirm = false;
            } else {
                this.fileList = [];
            }
        },
        async uploadRequest() {
            let formData = new FormData();
            formData.append('file', this.file);
            let res = await request({
                baseURL: '',
                url: '',
                methods: 'post',
                data: formData
            });
            if (res.resultCode == '00') {
                this.$message.success(res.resultData.message);
            } else {
                this.$message.success(res.resultData.message);
            }
        },
   
//  
       exportData(){
            const loading = this.$loading({
                lock:true,
                text:"     ,   ..."
            })
            var promise = request({
                url:"",
                method:"post",
                data:{},
                responseType:"blob"
            })
            promise.then(res=>{
                if(res&&res.size<=52){
                    this.$message.error("    ,    !")
                }else{
                    const link = document.createElement("a");
                    let blob = new Blob([res],{type:'application/vnd.ms-excel'})
                    link.style.display = "none"
                    link.href = URL.createObjectURL(blob)
                    link.setAttribute("download","    .xlsx")
                    document.body.appendChild(link)
                    link.click();
                    document.body.removeChild(link)
                }
                setTimeout(()=>{
                    loading.close()
                },2000)
            })
        }
//    
            downloadTemplate(){
                   let a =document.createElement('a')
                   a.download = '  '
                   a.href = './assets/template/   '
                   a.click()
            }
    }    
};




 
excel에서 표 가져오기로 내보내기


//..................  Excel.......................................................
importfxx(obj) {
    this.file = obj;
    var rABS = false; //              
    var f = this.file;
    var _this = this;
    var reader = new FileReader();
    //      
    FileReader.prototype.readAsBinaryString = function(f) {
        var binary = ''; //   
        var rABS = false; //              
        var wb; //       
        var reader = new FileReader();
        reader.onload = function(e) {
            //          
            var bytes = new Uint8Array(reader.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
                binary += String.fromCharCode(bytes[i]); //Unicode         
            }
            //     main.js   ,       ,    excel
            // var XLSX = require("xlsx");
            if (rABS) {
                wb = XLSX.read(btoa(fixdata(binary)), {
                    //    
                    type: 'base64'
                });
            } else {
                wb = XLSX.read(binary, {
                    type: 'binary'
                });
            }
            //excel        JSON,           Excel  
            _this.tableData = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
            _this.notifyInstance.close();
        };
        reader.readAsArrayBuffer(f);
    };
    if (rABS) {
        reader.readAsArrayBuffer(f);
    } else {
        reader.readAsBinaryString(f);
    }
},


//....................  Excel.................................................
getExcel() {
    require.ensure([], () => {
        const { export_json_to_excel } = require('../../introduce/Export2Excel.js')
        const tHeader = ['  ', '  ']
        const filterVal = ['name', 'age']
        const list = this.tableDate
        const data = this.formatJson(filterVal, list)
        export_json_to_excel(tHeader, data, '      ')
    })
},

formatJson(filterVal, jsonData) {
    return jsonData.map(v => filterVal.map(j => v[j]))
},

좋은 웹페이지 즐겨찾기