javascript下:从xls文件中导数据到数据库

/*****************************************************************************/ function CreateExcelApp(excelFileName,flag){ //objID为表格ID //需要在浏览器安全级别设置中启用ActiveX // Start Excel and get Application object. try{ oXL = new ActiveXObject("Excel.Application"); }catch(e){ alert(e.message); return ; } if (oXL == null){ alert("创建Excel文件失败,可能是您的计算机上没有正确安装Microsoft Office Excel软件或浏览器的安全级别设置过高!"); return; } try{ // Get a new workbook. oWB = oXL.Workbooks.Open(excelFileName); //for (i = 1; i <= oWB.Sheets.Count; i++){//如果有多表,就将该地方释放开 if(flag==1){ return ReadSheetData(1); }else if(flag==2){//jjywxx.xls return ReadSheetDataTA(1); } //} } catch (e){ alert(e.message); } oWB.Close();  oWB = null; oXL = null; } function ReadSheetDataTA(sheetname){ var oSheet = oWB.Sheets(sheetname); var result=""; var ye; var iStar=8; var tmpStr=""; for (i = 2; i <= oSheet.UsedRange.Rows.Count; i++){ for (j = 1; j <= oSheet.UsedRange.Columns.Count; j++){ if (oSheet.Cells(i, j).Value == undefined){ tmpStr = " "+"/b"; }else{ tmpStr = oSheet.Cells(i, j).Value+"/b"; } value=value+tmpStr; } if(value.length>0 ){ result=result +value+ "/f"; } value=""; } oSheet = null; oWB.Close();  oWB = null; oXL = null; return result; } function ReadSheetData(sheetname){ var oSheet = oWB.Sheets(sheetname); var result=""; var ye; var iStar=8; var tmpStr=""; for (i = 1; i <= oSheet.UsedRange.Rows.Count; i++){ if (oSheet.Cells(i, 1).Value == "公司名称"){ iStar=i+1; } } for (i = iStar; i <= oSheet.UsedRange.Rows.Count; i++){ for (j = 1; j <= oSheet.UsedRange.Columns.Count; j++){ if(j==1){ if (oSheet.Cells(i, 1).Value == undefined){ tmpStr=" "; }else{ tmpStr = oSheet.Cells(i, 1).Value+""; } } if(j==3){ if (oSheet.Cells(i, 3).Value == undefined){ value=" "; }else{ value = oSheet.Cells(i, 3).Value+"*"+tmpStr+""; } } if(j==7){ if (oSheet.Cells(i, 7).Value == undefined){ ye="0"; }else{ ye = oSheet.Cells(i, 7).Value+""; } } } if(value.length>0 && ye!= undefined && value!= undefined ){ result=result + value + "/t" + ye +"/r/n"; } } oSheet = null; oWB.Close();  oWB = null; oXL = null; return result; }

你可能感兴趣的:(JavaScript,function,Microsoft,Excel,null,application)