js读取本地excel文档数据

以下代码在需要设置IE安全级别低才能正常运行,贴出来主要是可以学习这方面的技术

<!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>
  <title>js读取本地excel文档数据 </title>
  <script>
	function readThis(){
		var tempStr = "";
		var filePath= document.all.upfile.value;
		var oXL = new ActiveXObject("Excel.application"); 
		var oWB = oXL.Workbooks.open(filePath);
		oWB.worksheets(1).select(); 
		var oSheet = oWB.ActiveSheet;
		try{
			for(var i=1;i<46;i++){
				if(oSheet.Cells(i,1).value =="null" || oSheet.Cells(i,2).value =="null" )
					break;
				var a = oSheet.Cells(i,1).value.toString()=="undefined"?"":oSheet.Cells(i,1).value;
				tempStr+=(
				"  "+oSheet.Cells(i,1).value+
				"  "+oSheet.Cells(i,2).value+
				"  "+oSheet.Cells(i,3).value+
				"  "+oSheet.Cells(i,4).value+
				"  "+oSheet.Cells(i,5).value+
				"  "+oSheet.Cells(i,6).value+"\n");
			}
		}
		catch(e){
			document.all.txtArea.value = tempStr;
		} 
		document.all.txtArea.value = tempStr;
		oXL.Quit();
		CollectGarbage();
	}
	</script>
 </head>

 <body>
	<input type="file" id="upfile" /><input type="button" onclick="readThis();" value="读取">
	<br />
	<textarea id="txtArea" cols=100 rows=30></textarea>
 </body>
</html>

原文地址:https://www.cnblogs.com/gsbhz/p/1877624.html