 
      
      
    2020-3-1 seo達人
純JS對頁面表格進行EXCEL導出
1.中間部分在style標簽那種可以使用css樣式對表格進行任意樣式的修改
2.在tableid.innerHTML中可以對表格中的內(nèi)容進行修改替換(其中放的內(nèi)容就是導出后的表格內(nèi)容)
3.需要引入xlsx.full.min.js文件
4.tableid為為表格的id
5.sheetName為下載后的文件名稱
        base64(excelFile) {
            return window.btoa(unescape(encodeURIComponent(excelFile)))
        },
        tableToExcel(tableid, sheetName) {
            var uri = 'data:application/vnd.ms-excel;base64,';
            var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"' +
                'xmlns=" gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'
                + '<x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>'
                + '</x:ExcelWorkbook></xml><![endif]-->' +
                ' <style type="text/css">' +
                '    .ivu-table td{\n' +
                '        /background-color: #FFFFFF;/\n' +
                '        /color: #fff;/\n' +
                '        /border:1px  solid #FFFFFF;/\n' +
                '    }\n' +
                '    /每行的基本樣式/\n' +
                '    .ivu-table-row td {\n' +
                '        color: #000000;\n' +
                '        min-width:50px;\n' +
                '    }\n' +
                '    /頭部th/\n' +
                '    .ivu-table-header th{\n' +
                '        color:\t#FFFFFF;\n' +
                '        font-weight: bold;\n' +
                '        background-color: rgb(98,167,249);\n' +
                '        min-width:50px;' +
                '        border:1px  solid #FFFFFF;' +
                '        position: relative;  \n' +
                '        top: expression(this.offsetParent.scrollTop);  \n' +
                '        z-index: 300; \n' +
                '    }\n' +
                '    /偶數(shù)行/\n' +
                '    .ivu-table-stripe-even td{\n' +
                '        background-color: #ffffff!important;\n' +
                '    }\n' +
                '    /奇數(shù)行/\n' +
                '    .ivu-table-stripe-odd td{\n' +
                '        background-color:#F0FFFF!important;\n' +
                '    }\n' +
                '    /選中某一行高亮/\n' +
                '    .ivu-table-row-highlight td {\n' +
                '        background-color: #d63333!important;\n' +
                '    }' +
                '</style>' +
                '</head><body ><table class="excelTable">{table}</table></body></html>';
            if (!tableid.nodeType) tableid = document.getElementById(tableid);
            tableid.innerHTML = tableid.innerHTML.replace('暫無篩選結(jié)果','')
            var ctx = {worksheet: sheetName || 'Worksheet', table: tableid.innerHTML};
            var a = document.createElement("a");
            a.download = sheetName + ".xls";
            a.href = uri + this.base64(this.format(template, ctx));;
            a.click();
        },
        format (s, c) {
            return s.replace(/{(\w+)}/g,
                function (m, p) {
                    return c[p];
                });
        }