1. 程式人生 > >js匯出Excel

js匯出Excel

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
    <title>HTML程式碼塊匯出Excel檔案(相容所有瀏覽器,支援設定檔名) </title>
  
    <style type="text/css">
      
    </style>
 
    <script language="javascript" type="text/javascript">
      
 
        //jQuery HTML匯出Excel檔案(相容IE及所有瀏覽器)
        function HtmlExportToExcel(tableid,filename) {
            if (getExplorer() == 'ie' || getExplorer() == undefined) {
                HtmlExportToExcelForIE(tableid, filename);
            }
            else {
                HtmlExportToExcelForEntire(tableid, filename)
            }
        }
        //IE瀏覽器匯出Excel
        function HtmlExportToExcelForIE(tableid, filename) {
            try {
                var winname = window.open('', '_blank', 'top=10000');
                var strHTML = document.getElementById(tableid).innerHTML;
 
                winname.document.open('application/vnd.ms-excel', 'export excel');
                winname.document.writeln(strHTML);
                winname.document.execCommand('saveas', '', filename + '.xls');
                winname.close();
 
            } catch (e) {
                alert(e.description);
            }
        }
        //非IE瀏覽器匯出Excel
        var HtmlExportToExcelForEntire = (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><!--[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 }
                document.getElementById("dlink").href = uri + base64(format(template, ctx));
                document.getElementById("dlink").download = name + ".xls";
                document.getElementById("dlink").click();
            }
        })()
        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';
            }
        }
    </script>
 
</head>
<body>
            <table border="1" id="dataTable"> 
			<tr>
			<td>王婷111</td>
			<td>一見傾城333 </td>
			</tr>
			<tr>
			<td>祈澈姑娘222</td>
			<td>Python開發者交流平臺44</td>
			</tr>
			<tr>
			<td>wwwangting888</td>
			<td>13661725475</td>
			</tr>
			</table> 
			
			 <div class="tools">            <button type="button" class="btn green" id="excell" onclick="HtmlExportToExcel('dataTable','cesi')">匯出考勤表格</button>        </div>
 <a id="dlink" style="display: none;"></a>

</body>
</html>