1. 程式人生 > >js 使用前端匯出excel,設定標題和頁尾,實現特殊表格需求

js 使用前端匯出excel,設定標題和頁尾,實現特殊表格需求

第一種:

js

//統計匯出
	$scope.statisExport=function(){
		toOutPutExcel('statisTable', '證件發放');
	};
	//匯出
	var idTmr;  
    function getExplorer() {  
        var explorer = window.navigator.userAgent ;  
        if(explorer.indexOf("MSIE")>=0){
        	//ie
            return 'ie';  
        }else if (explorer.indexOf("Firefox")>=0){ 
        	//firefox  
            return 'Firefox';  
        }else if(explorer.indexOf("Chrome")>=0){
        	//Chrome  
            return 'Chrome';  
        }else if(explorer.indexOf("Opera")>=0){
        	//Opera
            return 'Opera';  
        }else if(explorer.indexOf("Safari")>=0){  
        	//Safari  
            return 'Safari';  
        };
    };
  //設定匯出的excel的標題
    var excelTitle ="表格資料";
    function toOutPutExcel(tableid, title) {// 整個表格拷貝到EXCEL中
    	excelTitle =title;
    	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><!--[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)
    			table =  $("#"+table).clone();
	    		table = table[0];
	    		$(table).find(".ng-hide").remove();
    		var ctx = {
    			worksheet : excelTitle || 'Worksheet',
    			table : table.innerHTML
    		}
    		//window.location.href = uri + '77u/'+base64(format(template, ctx))
    		var titleDate= new Date().Format("yyyy-MM-dd hh-mm-ss");
    		document.getElementById("dlink").href = uri + '77u/'+base64(format(template, ctx));
            document.getElementById("dlink").download = excelTitle+titleDate+".xls";
            document.getElementById("dlink").click();
    	}
    })();

 html

<a id="dlink"  style="display:none"></a>
<table class="baselist" id="tableExps">
</table>

<a download="我是被匯出的表格.xls" href="#" onclick="return ExcellentExport.excel(this, 'datatable', 'Sheet Name Here');">Export table to Excel</a>