.net 将页面的table以excel形式导出

.net 将页面的table以excel形式导出

亲测有效

js代码如下 ,引用tableToExcel方法即可,参数为页面table的id属性

    function base64(content) {
    
    
        return window.btoa(unescape(encodeURIComponent(content)));
    }
    function tableToExcel(tableID) {
    
    
        var excelContent = $("#" + tableID).html();
        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 += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><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='10%'  border='1'>";
        excelFile += excelContent;
        excelFile += "</table></body>";
        excelFile += "</html>";
        var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
        var link = document.createElement("a");
        link.href = uri;
        link.style = "visibility:hidden";
        link.download = "导出的excel名称.xls";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }

猜你喜欢

转载自blog.csdn.net/qq_45286217/article/details/119882534