JS導出頁面table到Excel表格


<a href="javascript:;" id="export" >導出</a>
<table>
    <thead>
        <tr>
            <th>姓名</th>
            <th>手機</th>
        </tr>
    </thead>
    <tbody id="tableExcel">
        <tr>
            <td>扯淡1</td>
            <td>18888888888</td>
        </tr>
    </tbody>
</table>
<script>
    $("#export").click(function () {
        tableToExcel();
    })

    var tableToExcel = (function () {
        var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<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"><head><meta charset="UTF-8"><!--[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><body><table>{table}</table></body></html>'
        , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
        return function () {
            //根據ID獲取table表格HTML
            var table = document.getElementById("tableExcel");
            var ctx = { worksheet: 'Worksheet', table: table.innerHTML };
            document.getElementById("export").href = uri + base64(format(template, ctx));
            document.getElementById("export").download = '培訓申請管理.xls';
        }
    })()
</script>

轉載:https://blog.csdn.net/qq_31634167/article/details/83056744


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM