private void CreatingPositionsPreorder(int orderId, string path) { List <string> fiels = GetFileArray(path); using (PortalKATEKEntities db = new PortalKATEKEntities()) { foreach (var fiel in fiels.Where(a => a.Contains("~") == false)) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(fiel); IWorksheet worksheet = workbook.Worksheets[0]; int lenght = worksheet.Rows.Length; for (int i = 0; i < lenght; i++) { if (worksheet.Rows[i].Columns[1].DisplayText.Contains("PCAM") == true) { PlexiglassPositionsOrder pos = new PlexiglassPositionsOrder { id_PlexiglassOrder = orderId, positionNum = worksheet.Rows[i].Cells[0].Value, designation = worksheet.Rows[i].Cells[1].Value.Trim(), name = worksheet.Rows[i].Cells[2].Value.Trim(), index = worksheet.Rows[i].Cells[3].Value.Trim(), quentity = (int)worksheet.Rows[i].Cells[4].Number, square = 0.0, barcode = GetBarcode(worksheet.Rows[i].Cells[1].Value, worksheet.Rows[i].Cells[3].Value) }; if (double.IsNaN(worksheet.Rows[i].Cells[5].Number)) { try { pos.square = Convert.ToDouble(worksheet.Rows[i].Cells[5].DisplayText); } catch { try { pos.square = Convert.ToDouble(worksheet.Rows[i].Cells[5].DisplayText.Replace(".", ",")); } catch { } } } else { pos.square = worksheet.Rows[i].Cells[5].Number; } db.PlexiglassPositionsOrder.Add(pos); db.SaveChanges(); GetFileMaterials(pos.designation, path); worksheet.Rows[i].Cells[9].Value = pos.barcode; worksheet.Rows[i].Cells[9].Text = pos.barcode; } } workbook.Version = ExcelVersion.Excel2013; workbook.Save(); workbook.Close(); } } } }
public JsonResult AddOrder() { try { string login = HttpContext.User.Identity.Name; int returnId = 0; using (PortalKATEKEntities db = new PortalKATEKEntities()) { HttpPostedFileBase[] files = new HttpPostedFileBase[Request.Files.Count]; for (int i = 0; i < files.Length; i++) { files[i] = Request.Files[i]; } int[] ord = GetOrdersArray(Request.Form.ToString()); int typeMaterials = GetTypeMaterials(Request.Form.ToString()); int customer = GetCustomer(Request.Form.ToString()); int fileSize = files[0].ContentLength; var fileName = Path.GetFileName(files[0].FileName); byte[] fileByteArray = new byte[fileSize]; files[0].InputStream.Read(fileByteArray, 0, fileSize); string fileLocation = Path.Combine(Server.MapPath("~/temp"), fileName); if (!Directory.Exists(Server.MapPath("~/temp"))) { Directory.CreateDirectory(Server.MapPath("~/temp")); } files[0].SaveAs(fileLocation); string error = ""; using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(fileLocation); IWorksheet worksheet = workbook.Worksheets[0]; int lenght = worksheet.Rows.Length; for (int i = 0; i < lenght; i++) { if (worksheet.Rows[i].Columns[1].DisplayText.Contains("PCAM") == true) { PlexiglassPositionsOrder pos = new PlexiglassPositionsOrder { id_PlexiglassOrder = 0, positionNum = "", designation = worksheet.Rows[i].Cells[1].Value.Trim(), name = worksheet.Rows[i].Cells[2].Value.Trim(), index = worksheet.Rows[i].Cells[3].Value.Trim(), quentity = 0, square = 0.0, barcode = "" }; bool correct = GetFileMaterials(pos.designation); if (correct == false) { error += pos.designation + " - файл не найден " + "\n"; } } } workbook.Close(); } if (error != "") { return(Json(error, JsonRequestBehavior.AllowGet)); } foreach (var p in ord) { var order = new PlexiglassOrder { id_PZ_PlanZakaz = p, id_CMO_TypeProduct = typeMaterials, id_AspNetUsers = db.AspNetUsers.First(a => a.Email == login).Id, id_PlexiglassCompany = customer, datetimeCreate = DateTime.Now, folder = "", remove = false, note = "" }; db.PlexiglassOrder.Add(order); db.SaveChanges(); order.folder = CreateFolderAndFileForPreOrder(order.id, files); CreatingPositionsPreorder(order.id, order.folder); db.Entry(order).State = EntityState.Modified; db.SaveChanges(); new EmailPlexiglas(order, login, 0); } return(Json(returnId, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { logger.Debug("PlexiglasController/AddOrder: " + ex.Message); return(Json(0, JsonRequestBehavior.AllowGet)); } }