//读取excel表 function ReadExcel() { var str = ""; var colct = 0; var rowct = 0; var i,j,cname; var wkSheet = 1; if(document.getElementById("txtSheetName").value != "") wkSheet = document.getElementById("txtSheetName").value;//工作表的内容 var keyColrow = 1; if(document.getElementById("txtColRow").value != "") keyColrow = parseInt(document.getElementById("txtColRow").value,10); clearRowsOfTbExcel(); //增加表头 var trhead = TblExcel.insertRow(); var tdhead = trhead.insertCell(); tdhead.bgColor = "#e4f1ff"; tdhead.innerHTML = "Excel文件中的字段"; //读取excel表获取表头的信息构建表格 var pathstr = document.getElementById("fchk").value; var substr = pathstr.lastIndexOf("."); var suffix = pathstr.substring(substr+1,pathstr.length); if(suffix == "xls" || suffix == "csv") { var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.open(pathstr); //var oSheet = oWB.ActiveSheet; try { var rowCount = oWB.Worksheets(wkSheet).UsedRange.Cells.Rows.Count;//UsedRange var colCount = oWB.Worksheets(wkSheet).UsedRange.Columns.Count; document.getElementById("txtstartRow").value = 2; document.getElementById("txtEndRow").value = rowCount; //读取数据 for(i=1;i<=colCount;i++) { cname = oWB.Worksheets(wkSheet).Cells(keyColrow,i).value var newtr = TblExcel.insertRow(); var newtd1 = newtr.insertCell(); newtr.height = "25;" newtr.style.cursor = "hand"; newtr.onclick = function(){changeBgExcel(this)}; newtd1.Id = "te_"+i+"_0_"; newtd1.valign = "center"; if(i%2==0) newtr.style.backgroundColor = "#ffffff"; else newtr.style.backgroundColor = "#e4f1ff"; newtd1.innerHTML = cname; } //不读取隐藏的行列值 } catch(err) { alert("工作表名称或起始行指定不正确!"); } //解决调用时占用系统资源问题 oXL.DisplayAlerts = false; oWB.Close(); oXL.Quit(); oXL = null; idTmr = window.setInterval("Cleanup();",1); //解决调用时占用系统资源问题 } else { alert("请选择xls或者csv文件作为数据源"); } } //回收内存 function Cleanup() { window.clearInterval(idTmr); CollectGarbage(); }
javascript 读取客户端excel表
最新推荐文章于 2024-07-30 11:29:23 发布