public ActionResult UploadCustomersFile() { try { if (Request.Files.Count > 0) { HttpPostedFileBase uploadFile = Request.Files[0] as HttpPostedFileBase; if (uploadFile.ContentLength > 0) { //上传文件 string strDestFolder = HttpContext.Server.MapPath("/AttachFiles") + @"\" + DateTime.Now.ToString("yyyy-MM-dd"); if (!Directory.Exists(strDestFolder)) Directory.CreateDirectory(strDestFolder); string filePath = strDestFolder + @"\" + Guid.NewGuid().ToString() + Path.GetExtension(uploadFile.FileName); uploadFile.SaveAs(filePath); //读取数据 string strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; using (OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConnectionString)) { conn.Open(); using (DataTable dtExcelSchema = conn.GetSchema("Tables")) { string strSheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); string strQuery = "SELECT * FROM [" + strSheetName + "]"; OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn); DataSet ds = new DataSet(); adapter.Fill(ds, "Items"); if (ds.Tables.Count > 0) { if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Customer data = new Customer(); data.Name = ds.Tables[0].Rows[i][1].ToString(); data.FullName = ds.Tables[0].Rows[i][2].ToString(); data.WarningStock = int.Parse(ds.Tables[0].Rows[i][6].ToString()); data.StopStock = int.Parse(ds.Tables[0].Rows[i][7].ToString()); data.SettlementExpression = ds.Tables[0].Rows[i][8].ToString(); data.ValuationMode = ds.Tables[0].Rows[i][9].ToString(); data.GrossWeightRate = decimal.Parse(ds.Tables[0].Rows[i][10].ToString()); data.OwnOrganId = long.Parse(ds.Tables[0].Rows[i][11].ToString()); data.Remark = ds.Tables[0].Rows[i][12].ToString(); string strErrText; CustomerSystem customer = new CustomerSystem(); if (customer.InsertCustomer(data, new List<CustomerTransportPrice>(), new List<CustomerForceFeePrice>(), new List<CustomerStorageFeePrice>(), LoginAccountId, LoginStaffName, out strErrText) <= 0) { return Content(string.Format(InnoSoft.LS.Resources.Strings.ImportFailed, strErrText, i + 2)); } } } } } } } } return Content(string.Empty); } catch (Exception e) { return Content(e.Message); } }
public ActionResult NewCustomer(CustomerViewModel model) { if (ModelState.IsValid) { //创建数据 Customer data = new Customer(); data.Name = model.Name; data.FullName = model.FullName; data.WarningStock = model.WarningStock; data.StopStock = model.StopStock; data.SettlementExpression = model.SettlementExpression; data.ValuationMode = model.ValuationMode; data.GrossWeightRate = decimal.Parse(model.GrossWeightRate); data.OwnOrganId = model.OwnOrganId; data.Remark = model.Remark ?? string.Empty; List<CustomerTransportPrice> listTransportPrice = new List<CustomerTransportPrice>(); if (model.TransportPrices != null) { foreach (CustomerTransportPriceViewModel m in model.TransportPrices) { CustomerTransportPrice p = new CustomerTransportPrice(); p.CustomerId = m.CustomerId; p.StartCountry = m.StartCountry; p.StartProvince = m.StartProvince; p.StartCity = m.StartCity; p.DestCountry = m.DestCountry; p.DestProvince = m.DestProvince; p.DestCity = m.DestCity; p.MinTunnagesOrPiles = m.MinTunnagesOrPiles; p.MaxTunnagesOrPiles = m.MaxTunnagesOrPiles; p.StartTime = DateTime.Parse(m.StartTime); p.EndTime = DateTime.Parse(m.EndTime); p.CarType = m.CarType; p.TransportPrice = m.TransportPrice; p.RiverCrossingCharges = m.RiverCrossingCharges; listTransportPrice.Add(p); } } List<CustomerForceFeePrice> listForceFeePrice = new List<CustomerForceFeePrice>(); if (model.ForceFeePrices != null) { foreach (CustomerForceFeePriceViewModel m in model.ForceFeePrices) { CustomerForceFeePrice p = new CustomerForceFeePrice(); p.CustomerId = m.CustomerId; p.StartTime = DateTime.Parse(m.ForceFeePriceStartTime); p.EndTime = DateTime.Parse(m.ForceFeePriceEndTime); p.LoadingForceFeePrice = m.LoadingForceFeePrice; p.UnloadingForceFeePrice = m.UnloadingForceFeePrice; listForceFeePrice.Add(p); } } List<CustomerStorageFeePrice> listStorageFeePrice = new List<CustomerStorageFeePrice>(); if (model.StorageFeePrices != null) { foreach (CustomerStorageFeePriceViewModel m in model.StorageFeePrices) { CustomerStorageFeePrice p = new CustomerStorageFeePrice(); p.CustomerId = m.CustomerId; p.StartTime = DateTime.Parse(m.StorageFeePriceStartTime); p.EndTime = DateTime.Parse(m.StorageFeePriceEndTime); p.StorageFeePrice = m.StorageFeePrice; listStorageFeePrice.Add(p); } } //保存数据 string strErrText; CustomerSystem customer = new CustomerSystem(); if (customer.InsertCustomer(data, listTransportPrice, listForceFeePrice, listStorageFeePrice, LoginAccountId, LoginStaffName, out strErrText) > 0) { return Json(string.Empty); } else { return Json(strErrText); } } return View(model); }