public ActionResult UploadGoodsFile() { 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++) { Goods data = new Goods(); data.GoodsNo = ds.Tables[0].Rows[i][0].ToString(); data.Name = ds.Tables[0].Rows[i][1].ToString(); long nTypeId = 0; if (!long.TryParse(ds.Tables[0].Rows[i][2].ToString(), out nTypeId)) { return Content(string.Format(InnoSoft.LS.Resources.Strings.NotValidTypeId, i + 2)); } data.TypeId = nTypeId; data.SpecModel = ds.Tables[0].Rows[i][3].ToString(); data.GWeight = ds.Tables[0].Rows[i][4].ToString(); data.Grade = ds.Tables[0].Rows[i][5].ToString(); data.Brand = ds.Tables[0].Rows[i][6].ToString(); if (ds.Tables[0].Rows[i][7].ToString() != string.Empty) { decimal decPieceWeight = 0; if (!decimal.TryParse(ds.Tables[0].Rows[i][7].ToString(), out decPieceWeight)) { return Content(string.Format(InnoSoft.LS.Resources.Strings.NotValidPieceWeight, i + 2)); } } data.PieceWeight = ds.Tables[0].Rows[i][7].ToString(); data.Packing = ds.Tables[0].Rows[i][8].ToString(); data.Remark = ds.Tables[0].Rows[i][9].ToString(); string strErrText; DDSystem dd = new DDSystem(); if (!dd.InsertGoods(data, LoginAccountId, LoginStaffName, out strErrText)) { 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 NewGoods(GoodsViewModel model) { if (ModelState.IsValid) { //创建数据 Goods data = new Goods(); data.Name = model.Name; data.TypeId = model.TypeId; data.GoodsNo = model.GoodsNo; data.SpecModel = model.SpecModel; data.GWeight = model.GWeight; data.Grade = model.Grade; data.Brand = model.Brand; data.PieceWeight = model.PieceWeight; data.Packing = model.Packing; data.Remark = model.Remark; //保存数据 string strErrText; DDSystem dd = new DDSystem(); if (dd.InsertGoods(data, LoginAccountId, LoginStaffName, out strErrText)) { return Json(string.Empty); } else { return Json(strErrText); } } return View(model); }