美文网首页
util(cookie和导出excel)

util(cookie和导出excel)

作者: 人穷脸丑小前端 | 来源:发表于2020-03-09 10:41 被阅读0次

    //获取cookie、
    export const getCookie=(name)=>{
    let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
    return (arr[2]);
    else
    return null;
    };

    //设置cookie,增加到vue实例方便全局调用
    export const setCookie=(c_name, value, expiredays)=>{
    let exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    };

    //删除cookie
    export const delCookie=(name)=>{
    let exp = new Date();
    exp.setTime(exp.getTime() - 1);
    let cval = getCookie(name);
    if (cval != null)
    document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    };

    //导出excel
    export const exportExcel = (table, name)=>{
    let tableHtml;
    if(table.refs){ console.log(table.refs);
    let header=table.refs.header.querySelector('table').innerHTML; let body=table.refs.body.querySelector('tbody').outerHTML;
    tableHtml=header+body;
    }else {
    tableHtml=table.innerHTML;
    }
    let uri = 'data:application/vnd.ms-excel;base64,',
    template = <html> <head><meta charset="UTF-8"></head> <body><table border="1">${tableHtml}</table></body> </html>;

    let a=document.createElement('a');
    a.href=uri + window.btoa(unescape(encodeURIComponent(template)));
    a.download=name;
    a.click();
    document.removeChild(a);
    

    };

    相关文章

      网友评论

          本文标题:util(cookie和导出excel)

          本文链接:https://www.haomeiwen.com/subject/cxkudhtx.html