/** * Created by lenovo on 2016/5/18. */ var EmsMarkerOpt = function () { return { init: function () { $('#excel_import').live('click', function (e) { // var filePath = $("#excelFile").val(); // if (null != filePath && "" != filePath) { // if (filePath.indexOf(".xls") != filePath.length - 4 && filePath.indexOf(".xls") != filePath.length - 5) { // alert("请选择*.xls或*.xlsx文件"); // return; // } // importByExcel($("#excelFile").val()); //// //刷新列表页面就行了 // location.href = $("#context").val() + "/content/ems/ems.jsp"; // } $("#excelForm").ajaxSubmit({ type:"post", url:$("#context").val() +"/marker/excelMarkers.do", success:function(data){ if(data.success){ alert("上传成功"); }else{ alert(data.message); } }, error:function(){ alert("上传失败"); } }); }); // function importByExcel(filePath) { // //创建操作EXCEL应用程序的实例 // try { // var excelObject = new ActiveXObject("Excel.application"); // //打开指定路径的excel文件 // var excelFile = excelObject.Workbooks.open(filePath); // //操作第一个sheet(从一开始,而非零) // excelFile.worksheets(1).select(); // var excelSheet = excelFile.ActiveSheet; // //文件总行数 // var totalRows = excelSheet.usedrange.rows.count; // } catch (e) { // } // try { // //获取属性index // var userId = getPropertyIndex(excelSheet, "用户ID"); // var markerNo = getPropertyIndex(excelSheet, "标识器编号"); // var markerName = getPropertyIndex(excelSheet, "标识器名称"); // var area = getPropertyIndex(excelSheet, "三级区域"); // var line = getPropertyIndex(excelSheet, "三级线路"); // var objType = getPropertyIndex(excelSheet, "类型"); // var depth = getPropertyIndex(excelSheet, "埋深"); // var mileage1 = getPropertyIndex(excelSheet, "里程1"); // var mileage2 = getPropertyIndex(excelSheet, "里程2"); // var deployTime = getPropertyIndex(excelSheet, "安装时间"); // var deployUser = getPropertyIndex(excelSheet, "安装人员"); // var belongComp = getPropertyIndex(excelSheet, "权属单位"); // var longitudeIndex = getPropertyIndex(excelSheet, "经度"); // var latitudeIndex = getPropertyIndex(excelSheet, "纬度"); // var remark = getPropertyIndex(excelSheet, "备注"); // // var markerList = new Array(); // // //第一行是标题,从2开始 // for (var i = 2; i <= totalRows; i++) { // var markerDto = {}; // // 1.读取excel中的属性信息 // markerDto.userId = getNotNullNumberValue(excelSheet.Cells(i, userId).value); // markerDto.markerId = getNotNullValue(excelSheet.Cells(i, markerNo).value); // markerDto.markerName = getNotNullValue(excelSheet.Cells(i, markerName).value); // markerDto.featureId = getNotNullNumberValue(excelSheet.Cells(i, area).value); // markerDto.lineId = getNotNullNumberValue(excelSheet.Cells(i, line).value); // markerDto.markerObjectType = getNotNullValue(excelSheet.Cells(i, objType).value); // markerDto.depth = getNotNullNumberValue(excelSheet.Cells(i, depth).value); // markerDto.mileage = getNotNullValue(excelSheet.Cells(i, mileage1).value); // markerDto.mileage2 = getNotNullValue(excelSheet.Cells(i, mileage2).value); // markerDto.createTime = getNotNullValue(excelSheet.Cells(i, deployTime).value); // markerDto.creator = getNotNullValue(excelSheet.Cells(i, deployUser).value); // markerDto.ownerComp = getNotNullValue(excelSheet.Cells(i, deployUser).value); // markerDto.longitude = getNotNullValue(excelSheet.Cells(i, longitudeIndex).value); // markerDto.latitude = getNotNullValue(excelSheet.Cells(i, latitudeIndex).value); // markerDto.memo = getNotNullValue(excelSheet.Cells(i, remark).value); // markerList.push(markerDto); // } // // 2.导入到数据库 // $.ajax({ // type: "POST", // async: false, // url: $("#context").val() + "/marker/batchSave.do", // data: {"markers": JSON.stringify(markerList)}, // success: function (result) { // var result = eval("(" + result + ")"); // if (!result.success) { // alert("标识器入库失败," + result.msg); // } else { // alert("入库完成"); // } // // } // }); // // // } catch (e) { // } // //退出操作excel的实例对象 // excelObject.Application.Quit(); // //手动调用垃圾收集器 // CollectGarbage(); // }; function getPropertyIndex(excelSheet, propertyName) { var index = 1; while (true) { var value = excelSheet.Cells(1, index).value; if (!value) { return -1; } else if (value == propertyName) { return index; } index++; } } function getNotNullValue(inValue) { var outValue = ""; if (typeof inValue == "string") { inValue = inValue.replace(/\r\n/, ""); } if (null != inValue) { outValue = inValue; } return outValue; } function getNotNullNumberValue(inValue) { return inValue ? inValue : -1; } } } }();