1. 程式人生 > >js匯出tables為excel

js匯出tables為excel

下面例項是本人已django前端模版為例,使用時,將迴圈改為普通html標籤即可

{% load i18n %}
<input id="Button1" type="button" value="匯出EXCEL" onclick="javascript:method1('table_play_times_display')"/>
<a href="#" class="easyui-linkbutton" id="exportExcel" data-options="iconCls:'icon-search'"></a>
<div class="table-responsive" style="height:400px;overflow:scroll;">
    <table class="table" cellspacing="0" id="table_play_times_display">
        <caption>播放次數</caption>
        <thead>
        <tr>
            <th>{% trans 'Date' %}</th>
            <th>{% trans 'Play counts' %}</th>
            <th>{% trans 'Rise and fall' %}</th>
            <th>{% trans 'Profit' %}</th>
        </tr>
        </thead>
        <tbody>
        {% for i in report.date_data %}
            <tr>
                <td>{{ i.3 }}</td>
                <td>{{ i.1 }}</td>
                <td>{{ i.2 }}</td>
                <td>0</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
<script type="text/javascript" language="javascript">
    var idTmr;
    var filename = 'plays_count.xls';
    function getExplorer() {
        var explorer = window.navigator.userAgent;
        //ie  
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox  
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome  
        else if (explorer.indexOf("Chrome") >= 0) {
            return 'Chrome';
        }
        //Opera  
        else if (explorer.indexOf("Opera") >= 0) {
            return 'Opera';
        }
        //Safari  
        else if (explorer.indexOf("Safari") >= 0) {
            return 'Safari';
        }
    }
    function method1(tableid) {//整個表格拷貝到EXCEL中  
        if (getExplorer() == 'ie') {
            var curTbl = document.getElementById(tableid);
            var oXL = new ActiveXObject("Excel.Application");

            //建立AX物件excel  
            var oWB = oXL.Workbooks.Add();
            //獲取workbook物件  
            var xlsheet = oWB.Worksheets(1);
            //啟用當前sheet  
            var sel = document.body.createTextRange();
            sel.moveToElementText(curTbl);
            //把表格中的內容移到TextRange中  
            sel.select;
            //全選TextRange中內容  
            sel.execCommand("Copy");
            //複製TextRange中內容  
            xlsheet.Paste();
            //貼上到活動的EXCEL中  
            oXL.Visible = true;
            //設定excel可見屬性  

            try {
                var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
            } catch (e) {
                print("Nested catch caught " + e);
            } finally {
                oWB.SaveAs(fname);

                oWB.Close(savechanges = false);
                //xls.visible = false;  
                oXL.Quit();
                oXL = null;
                //結束excel程序,退出完成  
                //window.setInterval("Cleanup();",1);  
                idTmr = window.setInterval("Cleanup();", 1);

            }

        }
        else {
            tableToExcel(tableid)
        }
    }
    function Cleanup() {
        window.clearInterval(idTmr);
        CollectGarbage();
    }
    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">' +
                ' <style type="text/css">' +
                '.excelTable  {' +
                'border-collapse:collapse;' +
                ' border:thin solid #999; ' +
                '}' +
                '   .excelTable  th {' +
                '   border: thin solid #999;' +
                '  padding:20px;' +
                '  text-align: center;' +
                '  border-top: thin solid #999;' +
                ' background-color: #E6E6E6;' +
                '  }' +
                ' .excelTable  td{' +
                ' border:thin solid #999;' +
                '  padding:2px 5px;' +
                '  text-align: center;' +
                ' }</style>' +
                '<!--[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 (table, name) {
            if (!table.nodeType) table = document.getElementById(table);
            var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
            var exportExcel = document.getElementById("exportExcel");
            exportExcel.href = uri + base64(format(template, ctx));
            exportExcel.download = filename;
            exportExcel.click();
        }
    })()
</script>