JS导出

本实例是用vue.js取到json数据,使用js导出数据表格,需要引入xlsx.full.min.js

<script src="~/Content/js/SheetJS/xlsx.full.min.js"></script>

onExport() {
    const cur = this;
    let expotClu = ["客户姓名", "身份证号码", "营业部", "产品", "贷款金额", "贷款期数", "贷款日期", "剩余本金", "签约日期", "当前期数"];
    let column = ["CustName", "Idcard", "BranchName", "ProductName", "LoanAmount", "Periods", "LoanDate", "Balance", "CreateDate", "OrderNumber"];
    let qry = JSON.parse(JSON.stringify(cur.custQry));
    qry.PageSize = cur.totalCount;
    qry.PageNum = 1;
    VueHelper.vuepost(UrlList.AfterLoanUrl + "CustomerInfo/GetCustomerList", qry, (resp) => {
        if (resp.ErrCode != 0) {
            VueHelper.ShowMsg(resp.ResultMsg, "error");
        } else {
            cur.exportData = resp.Data.CustomerList;
            let newArr = [];
            for (let i = 0; i < cur.exportData.length; i++) {
                let newObj = {};
                for (let j = 0; j < expotClu.length; j++) {
                    let data = cur.exportData[i][column[j]];
                    if (expotClu[j].indexOf('日期') > -1) {
                        data = data == null ? "" : data.toString().substr(0, 10);
                    }
                    newObj[expotClu[j]] = data;
                }
                newArr.push(newObj);
            }
            const wb = { SheetNames: ["Sheet1"], Sheets: {}, Props: {} };
            wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(newArr);//通过json_to_sheet转成单页(Sheet)数据
            saveAs(new Blob([s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), "客户基本信息" + '.' + (wopts.bookType == "biff2" ? "xls" : wopts.bookType));
        }
    }, null, true);
},
    //这里的数据是用来定义导出的格式类型    const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };//xlsx格式    // const wopts = { bookType: 'csv', bookSST: false, type: 'binary' };//ods格式    // const wopts = { bookType: 'ods', bookSST: false, type: 'binary' };//ods格式    // const wopts = { bookType: 'xlsb', bookSST: false, type: 'binary' };//xlsb格式    // const wopts = { bookType: 'fods', bookSST: false, type: 'binary' };//fods格式    // const wopts = { bookType: 'biff2', bookSST: false, type: 'binary' };//xls格式    //如果使用 FileSaver.js 就不要同时使用以下函数    function saveAs(obj, fileName) {//当然可以自定义简单的下载文件实现方式        var tmpa = document.createElement("a");        tmpa.download = fileName || "下载";        tmpa.href = URL.createObjectURL(obj);//绑定a标签        tmpa.click();//模拟点击实现下载        setTimeout(function () {//延时释放            URL.revokeObjectURL(obj);//用URL.revokeObjectURL()来释放这个object URL        }, 100);    }    function s2ab(s) {        if (typeof ArrayBuffer !== "undefined") {            var buf = new ArrayBuffer(s.length);            var view = new Uint8Array(buf);            for (var i = 0; i != s.length; ++i) {                view[i] = s.charCodeAt(i) & 0xFF;            }            return buf;        } else {            var buf = new Array(s.length);            for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;            return buf;        }    } Date.prototype.Format = function (fmt) { //author: meizz 扩展时间Format属性        var o = {            "M+": this.getMonth() + 1, //月份            "d+": this.getDate(), //日            "h+": this.getHours(), //小时            "m+": this.getMinutes(), //分            "s+": this.getSeconds(), //秒            "q+": Math.floor((this.getMonth() + 3) / 3), //季度            "S": this.getMilliseconds() //毫秒        };        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));        for (var k in o)            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));        return fmt;    }

xlsx.full.min.js下载地址:
https://download.csdn.net/download/lixiaoer757/10407583

猜你喜欢

转载自blog.csdn.net/lixiaoer757/article/details/80278691