public ActionResult Submit(FormCollection formCollection) { HttpPostedFileBase file = Request.Files["UploadedFile"]; var datasList = new List <UploadDataVM>(); if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfCol = workSheet.Dimension.End.Column; var noOfRow = workSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) { var item = new UploadDataVM(); item.KPILevelCode = workSheet.Cells[rowIterator, 1].Value.ToSafetyString().ToUpper(); item.Value = workSheet.Cells[rowIterator, 2].Value.ToInt(); item.PeriodValue = workSheet.Cells[rowIterator, 3].Value.ToInt(); item.Year = workSheet.Cells[rowIterator, 4].Value.ToInt(); item.CreateTime = DateTime.Now; datasList.Add(item); } } return(Json(new UploadDAO().Add(datasList), JsonRequestBehavior.AllowGet)); } return(Json(false, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> Import(FormCollection formCollection) { HttpPostedFileBase file = Request.Files["UploadedFile"]; var datasList = new List <UploadDataVM>(); //var datasList2 = new List<UploadDataVM2>(); if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfCol = workSheet.Dimension.End.Column; var noOfRow = workSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) { var item = new UploadDataVM(); item.KPILevelCode = workSheet.Cells[rowIterator, 1].Value.ToSafetyString().ToUpper(); //item.KPIName = workSheet.Cells[rowIterator, 2].Value.ToSafetyString().ToUpper(); item.Value = workSheet.Cells[rowIterator, 3].Value.ToSafetyString(); item.TargetValue = workSheet.Cells[rowIterator, 4].Value.ToString() ?? "0"; item.PeriodValue = workSheet.Cells[rowIterator, 5].Value.ToInt(); item.Year = workSheet.Cells[rowIterator, 6].Value.ToInt(); //item.Area = workSheet.Cells[rowIterator, 7].Value.ToSafetyString(); //item.UpdateTime = workSheet.Cells[rowIterator, 8].Value.ToSafetyString().Trim(); //item.Remark = workSheet.Cells[rowIterator, 8].Value.ToSafetyString(); item.CreateTime = DateTime.Now; datasList.Add(item); } } var userprofile = Session["UserProfile"] as UserProfileVM; var model = await _dao.ImportData(datasList, userprofile.User.Alias, userprofile); NotificationHub.SendNotifications(); if (model.ListDataSuccess.Count > 0) { string content2 = System.IO.File.ReadAllText(Server.MapPath("~/Templates/UploadSuccessfully.html")); content2 = content2.Replace("{{{content}}}", "<b style='color:green'>Upload Data Successfully!</b><br/> Dear Updater, <br/> You just uploaded the KPIs as below list: "); var html2 = string.Empty; foreach (var item in model.ListDataSuccess.DistinctBy(x => x.KPIName)) { var area = new LevelDAO().GetNode(item.KPILevelCode); html2 += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{code}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> </tr>" .Replace("{{area}}", area) .Replace("{{code}}", item.KPILevelCode) .Replace("{{kpiname}}", item.KPIName); } var url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/workplace"; string from = ConfigurationManager.AppSettings["FromEmailAddress"].ToSafetyString(); content2 = content2.Replace("{{{html-template}}}", html2).Replace("{{{href}}}", url); Commons.SendMail(model.ListSendMail, "[KPI System] Upload Data succesfully!", content2, "Upload Data succesfully!"); } if (model.ListUploadKPIVMs.Count > 0) { string content = System.IO.File.ReadAllText(Server.MapPath("~/Templates/BelowTarget.html")); content = content.Replace("{{{content}}}", @"<b style='color:red'>Below Target!</b><br/>Dear Owner, <br/>Please add your comment and action plan because you did not archive kpi target as below list:"); var html = string.Empty; foreach (var item in model.ListUploadKPIVMs) { var area = new LevelDAO().GetNode(item.KPILevelCode); if (item.Week > 0) { html += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{code}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{week}}</td> </tr>" .Replace("{{area}}", area) .Replace("{{code}}", item.KPILevelCode) .Replace("{{kpiname}}", item.KPIName) .Replace("{{week}}", "Week " + item.Week.ToSafetyString()); } if (item.Month > 0) { html += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{code}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{month}}</td> </tr>" .Replace("{{area}}", area) .Replace("{{code}}", item.KPILevelCode) .Replace("{{kpiname}}", item.KPIName) .Replace("{{month}}", "Month " + item.Month.ToSafetyString()); } if (item.Quarter > 0) { html += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{code}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{quarter}}</td> </tr>" .Replace("{{area}}", area) .Replace("{{code}}", item.KPILevelCode) .Replace("{{kpiname}}", item.KPIName) .Replace("{{quarter}}", "Quarter " + item.Quarter.ToSafetyString()); } if (item.Year > 0) { html += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{code}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{year}}</td> </tr>" .Replace("{{area}}", area) .Replace("{{code}}", item.KPILevelCode) .Replace("{{kpiname}}", item.KPIName) .Replace("{{year}}", "Year " + item.Year.ToSafetyString()); } } var url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/workplace"; string from = ConfigurationManager.AppSettings["FromEmailAddress"].ToSafetyString(); content = content.Replace("{{{html-template}}}", html).Replace("{{{href}}}", url); Commons.SendMail(model.ListSendMail, "[KPI System] Below Target", content, "Below Target"); //signalR return(Json(model.Status, JsonRequestBehavior.AllowGet)); } return(Json(model.Status, JsonRequestBehavior.AllowGet)); } return(Json(false, JsonRequestBehavior.AllowGet)); }
public ActionResult Submit(FormCollection formCollection) { HttpPostedFileBase file = Request.Files["UploadedFile"]; var datasList = new List <UploadDataVM>(); //var datasList2 = new List<UploadDataVM2>(); if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfCol = workSheet.Dimension.End.Column; var noOfRow = workSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++) { var item = new UploadDataVM(); item.KPILevelCode = workSheet.Cells[rowIterator, 1].Value.ToSafetyString().ToUpper(); //item.KPIName = workSheet.Cells[rowIterator, 2].Value.ToSafetyString().ToUpper(); item.Value = workSheet.Cells[rowIterator, 3].Value.ToSafetyString(); item.TargetValue = workSheet.Cells[rowIterator, 4].Value.ToDouble(); item.PeriodValue = workSheet.Cells[rowIterator, 5].Value.ToInt(); //item.Year = workSheet.Cells[rowIterator, 6].Value.ToInt(); //item.Area = workSheet.Cells[rowIterator, 7].Value.ToSafetyString(); //item.UpdateTime = workSheet.Cells[rowIterator, 8].Value.ToSafetyString().Trim(); //item.Remark = workSheet.Cells[rowIterator, 8].Value.ToSafetyString(); item.CreateTime = DateTime.Now; datasList.Add(item); } } var model = new UploadDAO().UploadData(datasList); if (model.ListUploadKPIVMs.Count > 0) { string content = System.IO.File.ReadAllText(Server.MapPath("~/Templates/newEmail.html")); var html = string.Empty; foreach (var item in model.ListUploadKPIVMs) { html += @"<tr> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{area}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{kpiname}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{week}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{month}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{quarter}}</td> <td valign='top' style='padding:5px; font-family: Arial,sans-serif; font-size: 16px; line-height:20px;'>{{year}}</td> </tr>" .Replace("{{area}}", item.Area) .Replace("{{kpiname}}", item.KPIName) .Replace("{{week}}", item.Week.ToSafetyString()) .Replace("{{month}}", item.Month.ToSafetyString()) .Replace("{{quarter}}", item.Quarter.ToSafetyString()) .Replace("{{year}}", item.Year.ToSafetyString()); } content = content.Replace("{{{html-template}}}", html); var sessionUser = Session["UserProfile"] as UserProfileVM; string from = ConfigurationManager.AppSettings["FromEmailAddress"].ToSafetyString(); string password = ConfigurationManager.AppSettings["FromEmailPassword"].ToSafetyString(); string to = sessionUser.User.Email; if (sessionUser.User.Email.IsEmailFormat()) { to = sessionUser.User.Email.ToSafetyString(); } string subject = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToSafetyString(); MailMessage mail = new MailMessage(); mail.To.Add(to.ToString()); mail.From = new MailAddress(from, "KPI.App"); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = true; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.Priority = MailPriority.High; try { using (var smtp = new SmtpClient()) { smtp.Send(mail); } } catch (Exception ex) { Console.WriteLine(ex); } return(Json(model.Status, JsonRequestBehavior.AllowGet)); } return(Json(model.Status, JsonRequestBehavior.AllowGet)); } return(Json(false, JsonRequestBehavior.AllowGet)); }