javascript 將 table 導出 Excel ,可跨行跨列

<script language="JavaScript" type="text/javascript">
    	//jQuery HTML導出Excel文件(兼容IE及全部瀏覽器)
        function HtmlExportToExcel(tableid,file_name) {
            var filename =file_name; //'Book'
            if (getExplorer() == 'ie' || getExplorer() == undefined) {
                HtmlExportToExcelForIE(tableid, filename);
            }
            else {
                HtmlExportToExcelForEntire(tableid, filename)
            }
        }
        //IE瀏覽器導出Excel
        function HtmlExportToExcelForIE(tableid, filename) {
            try {             
                var curTbl = document.getElementById(tableid);  
	            var oXL;  
	            try{  
	                oXL = new ActiveXObject("Excel.Application"); //建立AX對象excel  
	            }catch(e){  
	                alert("沒法啓動Excel!\n\n若是您確信您的電腦中已經安裝了Excel,"+"那麼請調整IE的安全級別。\n\n具體操做:\n\n"+"工具 → Internet選項 → 安全 → 自定義級別 → 對沒有標記爲安全的ActiveX進行初始化和腳本運行 → 啓用");  
	                return false;  
	            }  
	            var oWB = oXL.Workbooks.Add(); //獲取workbook對象  
	            var oSheet = oWB.ActiveSheet;//激活當前sheet  
	            var sel = document.body.createTextRange();  
	            sel.moveToElementText(curTbl); //把表格中的內容移到TextRange中  
	            try{
	            	sel.select(); //全選TextRange中內容  
	            }catch(e1){
	            	e1.description
	            }
	            sel.execCommand("Copy");//複製TextRange中內容  
	            oSheet.Paste();//粘貼到活動的EXCEL中  
	            oXL.Visible = true; //設置excel可見屬性  
	            var fname = oXL.Application.GetSaveAsFilename(filename+".xls", "Excel Spreadsheets (*.xls), *.xls");  
	            oWB.SaveAs(fname);  
	            oWB.Close();  
	            oXL.Quit(); 

            } 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>
//調用
function ToExcel(){
     
     HtmlExportToExcel('table_main','Book'); 
}

//html部分   table_main爲table的id
<table  id="table_main"    > 

</table>
相關文章
相關標籤/搜索