1. 程式人生 > >js 讀取excel 檔案

js 讀取excel 檔案

<!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
	    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
	    <title>考勤excel讀取</title>

	    <!-- Bootstrap -->
	    <!-- 新 Bootstrap 核心 CSS 檔案 -->
	    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">

	    <!-- 可選的Bootstrap主題檔案(一般不用引入) 
	    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
	    -->


	    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
	    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	    <!--[if lt IE 9]>
	      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
	    <![endif]-->
	    <style type="text/css">.red{color:red} .blue{color: blue}</style>
		<script type="text/javascript">
			function ReadExcel() {
				var tempStr = "<table class='table table-bordered table-hover'><tr> <td>工號</td> <td>姓名</td> <td>上班時間</td> <td>下班時間</td> <td>一天工作時間/h</td> </tr>";
				//得到檔案路徑的值
				var filePath = document.getElementById("upfile").value;
				if(filePath == '') {alert('請選擇excel檔案');return;}
				//建立操作EXCEL應用程式的例項
				var oXL=null; 
				try{ 
					oXL = new ActiveXObject("Excel.Application"); 
				}catch(e){ 
					alert("請您開啟ActiveX控制元件,具體操作:單擊IE中的“工具”裡“Internet選項”,單擊“安全”中的“自定義級別”中“ActiveX控制元件和外掛”中,請選擇第三項為啟用即可。詳細錯誤:"+e.message); 
					return ; 
				} 
				if (oXL == null){ 
					alert("建立Excel檔案失敗,可能是您的計算機上沒有正確安裝Microsoft Office Excel軟體或瀏覽器的安全級別設定過高!"); 
					return; 
				}
				try{
					//開啟指定路徑的excel檔案
					oWB = oXL.Workbooks.Open(filePath); 
				}catch (e){
					alert("載入的Excel檔案路徑有錯誤!詳細:"+e.message); 
					return; 
				}
				try{
					//操作第一個sheet(從一開始,而非零)
					oWB.worksheets(1).select();
					var oSheet = oWB.ActiveSheet;
					//使用的行數
					var rows =  oSheet.usedrange.rows.count; 
					//if(!oSheet.Cells(51,1).value) alert('ssss');
					//for(j in oSheet) document.write(i,"====>",oSheet[j],"<br>");
					try {
						for (var i = 2; i <= rows; i++) {
							if (!oSheet.Cells(i, 1).value) continue;
							if ((i > 2) && ((oSheet.Cells(i, 3).value+"").split(' ')[0] == (oSheet.Cells(i-1, 3).value+"").split(' ')[0])) continue;
							var minTime = '';
							var maxTime = '';
							//找出最大最小值
							for (var j = i+1; j <= rows; j++) {
								if (!oSheet.Cells(j, 1).value) continue;
								// alert(oSheet.Cells(i, 3).value.toString());
								// alert(oSheet.Cells(i, 3).value);return;
								if ((oSheet.Cells(i, 3).value+"").split(' ')[0] == (oSheet.Cells(j, 3).value+"").split(' ')[0]){
									if(dateStr(oSheet.Cells(j, 3).value).getTime() >  dateStr(oSheet.Cells(i, 3).value).getTime()){
										minTime = oSheet.Cells(i, 3).value;
										maxTime = oSheet.Cells(j, 3).value;
									}else{
										minTime = oSheet.Cells(j, 3).value;
										maxTime = oSheet.Cells(i, 3).value;
									}
								}else continue;
							}
							var hour = Math.floor((dateStr(maxTime).getTime()-dateStr(minTime).getTime())/1000/3600);
							if (isNaN(hour)) {continue;}
							var timeStr = hour-1+'小時'+ Math.floor(((dateStr(maxTime).getTime()-dateStr(minTime).getTime())/1000/3600-hour)*60)+'分';
							if (hour < 9) {timeStr+=' <span class="red">不足八小時</span>'};
							tempStr += ("<tr> <td>" + oSheet.Cells(i, 1).value + "</td> <td>" + oSheet.Cells(i, 2).value + "</td> <td>" + minTime 
								+ "</td> <td>" + maxTime + "</td> <td>" + timeStr + "</td></tr>");
						}
					} catch(e) {
						alert(e.message);
					}
					document.getElementById("txtArea").innerHTML = tempStr;
					
				} catch (e){
					alert(e.message);
				}
				oWB.Close();//不close工作簿的話,後果還是挺嚴重的。 
				//退出操作excel的例項物件
				oXL.Application.Quit();
				oWB = null; 
				oXL = null; 
				//手動呼叫垃圾收集器
				CollectGarbage();
			}
			dateStr = function(s){
				return new Date(s.replace(/-/g, "/"))
			}
		</script>
		</head>

	<body>
		<div class="alert alert-danger" role="alert">
			<span class="red">檔案大小不能超過<span class="blue">1M</span></span><br>
			<span class="red">需要使用<span class="blue">IE</span>開啟 </span><br>
			<span class="red">需要需要安裝<span class="blue">excel</span>軟體 </span><br>
			<span class="red">請您<span class="blue">開啟ActiveX控制元件</span>,具體操作:單擊IE中的“工具”裡“Internet選項”,單擊“安全”中的“自定義級別”中“ActiveX控制元件和外掛”中,請選擇第三項為啟用即可</span><br>
			<span class="red">如果建立Excel檔案失敗,可能是您的計算機上沒有正確安裝<span class="blue">Microsoft Office Excel軟體</span>或瀏覽器的安全級別設定過高!</span>
		</div>
		<div style="padding:10px;">
			<input type="file" id="upfile"  />
			<br>
			<input type="button" onclick="ReadExcel();" class="btn btn-info" value="讀取">
			<br>
			<br>
		</div>
		<div id="txtArea" style="width:100%;height:auto;border: 1px solid red"></div>
	</body>
</html>