Vue blob excel export data

    downloadFile (res, fileName) {
        let data = res.data
        let url = window.URL.createObjectURL(new Blob([data], {
            type: "application/vnd.ms-excel"
        }))
        if ("download" in document.createElement("a")) {
            const a = document.createElement("a")
            a.href = url
            a.download = fileName
            a.style.display = "none"
            document.body.appendChild(a)
            a.click()
            URL.revokeObjectURL(a.href)
            document.body.removeChild(a)
        }
    },
    exportData() {
        exportData('/ih/statistic/consultOrder/excel', this.params).then(res => {
            if (res) {
                let fileName = "咨询订单统计表.xlsx"        
                this.downloadFile(res, fileName)
            } else {
                this.$message({
                    type: 'error',
                    message: res.msg
                })
            }
        })
    },

In addition, when the export fails, how to prompt the json information

        if (type == 'application/vnd.ms-excel') {
            return res;
        } else if (type == 'application/json') {
            const reader = new FileReader()
            reader.readAsText(data)
            reader.onload = function (e) {
                let obj = JSON.parse(e.target.result)//此处的msg就是后端返回的msg内容
                if (obj) {
                    data = obj
                }
                var logoutCodeArr = [610, 611];
                if (~logoutCodeArr.indexOf(data.code)) {
                    var loginPath = getLoginPath();
                    vm.$router.push(loginPath);
                }
                if (data.msg) {
                    alerts(data.msg);
                }
            }
        } else {
            var err = new Error();
            err.msg = "服务器内部错误";
            return Promise.reject(err);
        }

 

Guess you like

Origin blog.csdn.net/WDCCSDN/article/details/103903917