public ActionResult AjaxUploadStaionUpdateExcel() { HttpPostedFileBase upload = Request.Files["upLoadFile"]; try { if (Request.Cookies["User"] != null) { HttpCookie aCookie = Request.Cookies["User"]; string realname = Server.UrlDecode(aCookie["RealName"]); int colnum = 0, totalnum = 0; if (upload != null && upload.ContentLength > 0) { string filePath = @"D:\temp\Station.xlsx"; upload.SaveAs(filePath); using (InfoManageDataContext context = new InfoManageDataContext()) { #region EquipmentsStorage string InfoManageConnectionStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES\""; DataSet StationsDS = new DataSet(); using (OleDbConnection StationsConn = new OleDbConnection(InfoManageConnectionStr)) { string strCom = "SELECT * FROM [Sheet1$]"; StationsConn.Open(); OleDbDataAdapter WeatherPhenomenonCommand = new OleDbDataAdapter(strCom, StationsConn); WeatherPhenomenonCommand.Fill(StationsDS, "[Sheet1]"); StationsConn.Close(); } DataTable StationsDSDataTable = StationsDS.Tables[0]; foreach (DataRow row in StationsDSDataTable.Rows) { string storagename = row.ItemArray[4].ToString().Trim(); StoragesInfo sinfo = context.StoragesInfos.Where(c => c.BelongingName.Equals(storagename)).FirstOrDefault(); if (sinfo != null) { string oldstationnum = row.ItemArray[0].ToString().Trim(); Station oldstation = context.Stations.Where(c => c.StationNum.Equals(oldstationnum)).FirstOrDefault(); if (oldstation == null) { //string tmp = row.ItemArray[1].ToString().Trim(); Station station = new Station(); station.StationNum = row.ItemArray[0].ToString().Trim(); station.StationBuildDate = DateTime.ParseExact(row.ItemArray[1].ToString().Trim(), AllConsts.HOUR_MINUTE_SHORT, System.Globalization.DateTimeFormatInfo.CurrentInfo); station.StationName = row.ItemArray[2].ToString().Trim(); station.StationAdrass = row.ItemArray[3].ToString().Trim(); station.StoragesInfoID = sinfo.StoragesInfoID; station.transportManageType = Convert.ToInt32(row.ItemArray[5].ToString().Trim()); station.StationClass = Convert.ToInt32(row.ItemArray[6].ToString().Trim()); station.StationLetterCode = row.ItemArray[7].ToString().Trim(); station.StationType = row.ItemArray[8].ToString().Trim(); station.StationManufacturer = row.ItemArray[9].ToString().Trim(); station.StationLongitude = row.ItemArray[10].ToString().Trim(); station.StationLatitude = row.ItemArray[11].ToString().Trim(); station.StationAltitude = row.ItemArray[12].ToString().Trim(); station.ElementsNum = Convert.ToInt32(row.ItemArray[13].ToString().Trim()); station.samplingMod = Convert.ToInt32(row.ItemArray[14].ToString().Trim()); station.windDirectorHigh = row.ItemArray[15].ToString().Trim(); station.windSpeederHigh = row.ItemArray[16].ToString().Trim(); station.SIM = row.ItemArray[17].ToString().Trim(); station.IP = row.ItemArray[18].ToString().Trim(); station.frontPhoto = row.ItemArray[19].ToString().Trim(); station.overallView = row.ItemArray[20].ToString().Trim(); station.powerSupplyMod = Convert.ToInt32(row.ItemArray[21].ToString().Trim()); station.Operator = row.ItemArray[22].ToString().Trim(); station.LastModifier = AllConsts.adminName; station.LastModifyDate = DateTime.Now; station.BatteryStartDate = station.StationBuildDate; station.SIMRenewFeeDate = station.StationBuildDate; context.Stations.Add(station); colnum++; } else { oldstation.StationBuildDate = DateTime.ParseExact(row.ItemArray[1].ToString().Trim(), AllConsts.HOUR_MINUTE_SHORT, System.Globalization.DateTimeFormatInfo.CurrentInfo); oldstation.StationName = row.ItemArray[2].ToString().Trim(); oldstation.StationAdrass = row.ItemArray[3].ToString().Trim(); oldstation.StoragesInfoID = sinfo.StoragesInfoID; oldstation.transportManageType = Convert.ToInt32(row.ItemArray[5].ToString().Trim()); oldstation.StationClass = Convert.ToInt32(row.ItemArray[6].ToString().Trim()); oldstation.StationLetterCode = row.ItemArray[7].ToString().Trim(); oldstation.StationType = row.ItemArray[8].ToString().Trim(); oldstation.StationManufacturer = row.ItemArray[9].ToString().Trim(); oldstation.StationLongitude = row.ItemArray[10].ToString().Trim(); oldstation.StationLatitude = row.ItemArray[11].ToString().Trim(); oldstation.StationAltitude = row.ItemArray[12].ToString().Trim(); oldstation.ElementsNum = Convert.ToInt32(row.ItemArray[13].ToString().Trim()); oldstation.samplingMod = Convert.ToInt32(row.ItemArray[14].ToString().Trim()); oldstation.windDirectorHigh = row.ItemArray[15].ToString().Trim(); oldstation.windSpeederHigh = row.ItemArray[16].ToString().Trim(); oldstation.SIM = row.ItemArray[17].ToString().Trim(); oldstation.IP = row.ItemArray[18].ToString().Trim(); oldstation.frontPhoto = row.ItemArray[19].ToString().Trim(); oldstation.overallView = row.ItemArray[20].ToString().Trim(); oldstation.powerSupplyMod = Convert.ToInt32(row.ItemArray[21].ToString().Trim()); oldstation.Operator = row.ItemArray[22].ToString().Trim(); oldstation.LastModifier = AllConsts.adminName; oldstation.LastModifyDate = DateTime.Now; colnum++; } context.SaveChanges(); totalnum++; } } #endregion if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } } return Json("总" + totalnum + "记录,导入成功" + colnum + "记录", JsonRequestBehavior.AllowGet); } return Json(-1, JsonRequestBehavior.AllowGet); } catch { return Json(-2, JsonRequestBehavior.AllowGet); } }
public ActionResult AjaxAddNewStation( string StationNum, string StationBuildDate, string StationName, string StationAdrass, int StoragesInfoID, int transportManageType, int StationClass, int ElementsNum, int samplingMod, string windDirectorHigh, string windSpeederHigh, string StationLetterCode, string StationType, string StationManufacturer, string StationLongitude, string StationLatitude, string StationAltitude, string SIM, string IP, int powerSupplyMod, string BatteryStartDate, string SIMRenewFeeDate, string Operator) { try { if (Request.Cookies["User"] != null) { HttpCookie aCookie = Request.Cookies["User"]; ViewBag.RealName = Server.UrlDecode(aCookie["RealName"]); int id = Convert.ToInt32(Server.UrlDecode(aCookie["ID"])); using (MemberShipDataContext context = new MemberShipDataContext()) { UserProfile usr = context.UserProfiles.Find(id); if (null != usr) { using (InfoManageDataContext InfoManageContext = new InfoManageDataContext()) { Station newStation = new Station(); newStation.StationNum = StationNum.Trim(); newStation.StationBuildDate = DateTime.ParseExact(StationBuildDate, AllConsts.HOUR_MINUTE, System.Globalization.DateTimeFormatInfo.CurrentInfo); newStation.StationName = StationName; newStation.StationAdrass = StationAdrass; newStation.StoragesInfoID = StoragesInfoID; newStation.transportManageType = transportManageType; newStation.StationClass = StationClass; newStation.StationLetterCode = StationLetterCode; newStation.StationType = StationType; newStation.StationManufacturer = StationManufacturer; newStation.StationLongitude = StationLongitude; newStation.StationLatitude = StationLatitude; newStation.StationAltitude = StationAltitude; newStation.ElementsNum = ElementsNum; newStation.samplingMod = samplingMod; newStation.windDirectorHigh = windDirectorHigh; newStation.windSpeederHigh = windSpeederHigh; newStation.SIM = SIM; newStation.IP = IP; newStation.powerSupplyMod = powerSupplyMod; newStation.Operator = Operator; newStation.LastModifier = newStation.Operator; newStation.LastModifyDate = DateTime.Now; if (newStation.powerSupplyMod == (int)AllConsts.powerSupplyMod.SolarEnergy) newStation.BatteryStartDate = DateTime.ParseExact(BatteryStartDate, AllConsts.HOUR_MINUTE, System.Globalization.DateTimeFormatInfo.CurrentInfo); else newStation.BatteryStartDate = DateTime.ParseExact(AllConsts.MAX_DATE_HOUR_MINUTE, AllConsts.HOUR_MINUTE, System.Globalization.DateTimeFormatInfo.CurrentInfo); if (newStation.SIM.Trim() != "") newStation.SIMRenewFeeDate = DateTime.ParseExact(SIMRenewFeeDate, AllConsts.HOUR_MINUTE, System.Globalization.DateTimeFormatInfo.CurrentInfo); else newStation.SIMRenewFeeDate = DateTime.ParseExact(AllConsts.MAX_DATE_HOUR_MINUTE, AllConsts.HOUR_MINUTE, System.Globalization.DateTimeFormatInfo.CurrentInfo); Station oldstation = InfoManageContext.Stations.Where(c => c.StationNum.Equals(newStation.StationNum)).FirstOrDefault(); if (oldstation == null) InfoManageContext.Stations.Add(newStation); else { oldstation.StationBuildDate = newStation.StationBuildDate; oldstation.StationName = newStation.StationName; oldstation.StationAdrass = newStation.StationAdrass; oldstation.transportManageType = newStation.transportManageType; oldstation.StationClass = newStation.StationClass; oldstation.StationLetterCode = newStation.StationLetterCode; oldstation.StationType = newStation.StationType; oldstation.StationManufacturer = newStation.StationManufacturer; oldstation.StationLongitude = newStation.StationLongitude; oldstation.StationLatitude = newStation.StationLatitude; oldstation.StationAltitude = newStation.StationAltitude; oldstation.ElementsNum = newStation.ElementsNum; oldstation.samplingMod = newStation.samplingMod; oldstation.windDirectorHigh = newStation.windDirectorHigh; oldstation.windSpeederHigh = newStation.windSpeederHigh; oldstation.SIM = newStation.SIM; oldstation.IP = newStation.IP; oldstation.powerSupplyMod = newStation.powerSupplyMod; oldstation.Operator = newStation.Operator; oldstation.BatteryStartDate = newStation.BatteryStartDate; oldstation.SIMRenewFeeDate = newStation.SIMRenewFeeDate; oldstation.StoragesInfoID = StoragesInfoID; oldstation.LastModifier = oldstation.Operator; oldstation.LastModifyDate = DateTime.Now; } UserAction newaction = new UserAction(); newaction.ID = usr.ID; newaction.RealName = usr.RealName; newaction.ActionName = "AjaxSetTurnInStorageList"; newaction.CreateTime = DateTime.Now; newaction.LastModifyTime = newaction.CreateTime; InfoManageContext.UserActions.Add(newaction); InfoManageContext.SaveChanges(); return Json("OK", JsonRequestBehavior.AllowGet); } } } } return Json("NO", JsonRequestBehavior.AllowGet); } catch { return Json("NO", JsonRequestBehavior.AllowGet); } }