public ActionResult MaterialOperate(MaterialInfoModel model) { var result = MaterialBusiness.SaveMaterial(model, this.LoginUser); return(Json(result)); }
/// <summary> /// Test /// </summary> /// <param name="file"></param> /// <returns></returns> public ActionResult UploadMaterialFiles() { var result = new ResultInfoModel() { IsSuccess = false }; StringBuilder strbuild = new StringBuilder(); string FileName; string savePath; HttpPostedFileBase file = Request.Files["file"]; if (file == null || file.ContentLength <= 0) { result.Message = "please choose file"; return(Content(JsonHelper.JsonSerializer(result))); } else { string fileName = Path.GetFileName(file.FileName); int filesize = file.ContentLength; //获取上传文件的大小单位为字节byte string fileEx = Path.GetExtension(fileName); //获取上传文件的扩展名 string NoFileName = Path.GetFileNameWithoutExtension(fileName); //获取无扩展名的文件名 int Maxsize = 4000 * 1024; //定义上传文件的最大空间大小为4M string FileType = ".xls,.xlsx"; //定义上传文件的类型字符串 FileName = NoFileName + fileEx; if (!FileType.Contains(fileEx)) { result.Message = "please upload .xls and .xlsx"; return(Content(JsonHelper.JsonSerializer(result))); } if (filesize >= Maxsize) { result.Message = string.Format("file size can't big than {0}", Maxsize); return(Content(JsonHelper.JsonSerializer(result))); } string path = Server.MapPath("~/App_Data/uploads"); savePath = Path.Combine(path, FileName); file.SaveAs(savePath); string strConn; strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + savePath + ";Extended Properties=Excel 12.0;"; using (OleDbConnection conn = new OleDbConnection(strConn)) { conn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn); DataSet myDataSet = new DataSet(); try { myCommand.Fill(myDataSet, "ExcelInfo"); } catch (Exception ex) { result.Message = ex.Message; return(Content(JsonHelper.JsonSerializer(result))); } DataTable table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable(); try { for (int i = 0; i < table.Rows.Count; i++) { MaterialInfoModel model = new MaterialInfoModel(); model.MIProcessType = table.Rows[i][0].ToString(); model.MICustomer = table.Rows[i][1].ToString(); model.MISapPN = table.Rows[i][2].ToString(); model.MIProductName = table.Rows[i][3].ToString(); model.MIInjectionMC = table.Rows[i][4].ToString(); model.MICustomerPN = table.Rows[i][5].ToString(); model.MICavity = DataConvertHelper.ToInt(table.Rows[i][6].ToString(), 0); model.MICycletime = DataConvertHelper.ToDecimal(table.Rows[i][7].ToString(), 0); model.MICycletimeCav = DataConvertHelper.ToDecimal(table.Rows[i][8].ToString(), 0); model.MIStandardHeadcount = DataConvertHelper.ToInt(table.Rows[i][9].ToString(), 0); model.MTStandardScrap = table.Rows[i][10].ToString(); model.MIMaterialPN = table.Rows[i][11].ToString(); model.MICavityG = DataConvertHelper.ToDecimal(table.Rows[i][12].ToString(), 0); model.MIMoldNo = table.Rows[i][13].ToString(); model.MIAssAC = table.Rows[i][14].ToString(); model.MIWorkOrder = table.Rows[i][15].ToString(); MaterialBusiness.SaveMaterial(model, this.LoginUser); var insertResult = MaterialBusiness.SaveMaterial(model, LoginUser); } result.IsSuccess = true; } catch (Exception ex) { result.Message = ex.Message; return(Content(JsonHelper.JsonSerializer(result))); } conn.Close(); } return(Content(JsonHelper.JsonSerializer(result))); } }