public ActionResult ImportMachine(int type) { var result = new ImportUploadResult() { IsSuccess = false }; var invalidlist = new List <InvalidData>(); 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++) { var datevalid1 = false; var datevalid2 = false; var invalid = new InvalidData(); if (!string.IsNullOrEmpty(table.Rows[i][6].ToString())) { datevalid1 = DataConvertHelper.IsDateTime(table.Rows[i][6].ToString()); if (!datevalid1) { invalid.Key = table.Rows[i][2].ToString(); invalid.Value1 = table.Rows[i][6].ToString(); } } if (!string.IsNullOrEmpty(table.Rows[i][12].ToString())) { datevalid2 = DataConvertHelper.IsDateTime(table.Rows[i][12].ToString()); if (!datevalid2) { invalid.Key = table.Rows[i][2].ToString(); invalid.Value2 = table.Rows[i][12].ToString(); } } if (!datevalid1 && !datevalid2) { invalidlist.Add(invalid); continue; } var model = new MaToolModel(); model.BMClassification = type; model.BMEquipmentName = table.Rows[i][0].ToString(); model.BMEquipmentNo = table.Rows[i][1].ToString(); model.BMFixtureNo = table.Rows[i][2].ToString(); model.BMType = table.Rows[i][3].ToString(); model.BMSerialNumber = table.Rows[i][4].ToString(); model.BMQuantity = DataConvertHelper.ToInt(table.Rows[i][5].ToString(), 0); model.BMManufacturedDate = table.Rows[i][6].ToString(); model.BMPower = table.Rows[i][7].ToString(); model.BMOutlineDimension = table.Rows[i][8].ToString(); model.BMAbility = table.Rows[i][9].ToString(); model.BMNeedPressureAir = DataConvertHelper.GetYesOrNoValue(table.Rows[i][10].ToString()); model.BMNeedCoolingWater = DataConvertHelper.GetYesOrNoValue(table.Rows[i][11].ToString()); model.BMIncomingDate = table.Rows[i][12].ToString(); model.BMRemarks = table.Rows[i][13].ToString(); var inserResult = MaterialBusiness.SaveMaTool(model, LoginUser); //if (type != 4) //{ // MachineTool(type, table, i); //} //else { // MaterialTool(type, table, i); //} } result.invalidData = invalidlist; result.IsSuccess = true; } catch (Exception ex) { result.Message = ex.Message; return(Content(JsonHelper.JsonSerializer(result))); } conn.Close(); } return(Content(JsonHelper.JsonSerializer(result))); }
public ActionResult MaToolOperate(MaToolModel model) { var result = MaterialBusiness.SaveMaTool(model, this.LoginUser); return(Json(result)); }