将dom导出为word 、excel

ceshi(){
            this.ToWorld();
            this.ToExcel();
        },
        base64(content){
            return window.btoa(unescape(encodeURIComponent(content)));
        },
        ToWorld(){
            var worldContent = $('#statementBox').html();//NumberFormatLocal = "@"style='mso-number-format: @;'
            var worldFile = `<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:world' xmlns='http://www.w3.org/TR/REC-html40'>`
            worldFile += ` <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>`
            worldFile += `<body><h2 style='text-align: center;width: 100%'>测试</h2>`
            worldFile += worldContent;
            worldFile += `</body>`;
            worldFile +="</html>"
            console.log(worldContent)
            var link = "data:application/vnd.ms-world;base64," + this.base64(worldFile);
            var a = document.createElement("a");
            a.download = "测试.doc";
            a.href = link;
            a.click();
        },
        ToExcel(){
            var excelContent = $('#statementBox').html();//NumberFormatLocal = "@"style='mso-number-format: @;'
            var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
            excelFile += "<head><!--[if 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]--></head>";
            excelFile += "<body><table width='80%'  border='1'><h2 style='text-align: center;width: 100%'>测试</h2>";
            excelFile += excelContent;
            excelFile += "</table></body>";
            excelFile += "</html>";
            var link = "data:application/vnd.ms-excel;base64," + this.base64(excelFile);
            var a = document.createElement("a");
            a.download = "测试.xls";
            a.href = link;
            a.click();
        },
posted @ 2020-12-29 15:29  一条丶小咸鱼  阅读(369)  评论(0编辑  收藏  举报