public ActionResult Generate(HttpPostedFileBase excelFile = null, string nodeName = null) { if (excelFile == null || excelFile.ContentLength <= 0 || string.IsNullOrWhiteSpace(nodeName)) { return(RedirectToAction("Index")); } var path = Path.GetTempFileName(); ViewBag.ErrorMessage = path + Environment.NewLine; try { excelFile.SaveAs(path); var posList = new ExcelPositionList(path); if (!posList.LoadAiSheet()) { ViewBag.ErrorMessage += "Ошибка загрузки страницы AI из Excel\n" + Environment.NewLine; } if (!posList.LoadAoSheet()) { ViewBag.ErrorMessage += "Ошибка загрузки страницы AO из Excel\n" + Environment.NewLine; } if (!posList.LoadDioSheet()) { ViewBag.ErrorMessage += "Ошибка загрузки страницы DIO из Excel\n" + Environment.NewLine; } var binDirectoryPath = Server.MapPath("~/bin"); var p = AppDomain.CurrentDomain.BaseDirectory; var shemaFilePath = Path.Combine(binDirectoryPath, @"RSView\ImportExport\POSITIONLIST.xml"); var shema = XElement.Load(shemaFilePath); var converter = new RSViewPositionListConverter(posList, shema, nodeName); var csvGenerator = new CsvGenerator(converter.ConvertAllPositionsToRsViewTags(), nodeName); var fileData = csvGenerator.GetZipStream().ToArray(); var fileName = csvGenerator.ZipFileName; //Response.AppendHeader("Content-Disposition", ); return(File(fileData, MediaTypeNames.Application.Zip, fileName)); } catch (Exception ex) { System.IO.File.Delete(path); ViewBag.ErrorMessage += ex.ToString(); return(View()); } finally { System.IO.File.Delete(path); } }
public ActionResult MessagesUpdate(HttpPostedFileBase excelFile = null, int?plcId = null) { if (excelFile == null || excelFile.ContentLength <= 0 || plcId == null) { return(RedirectToAction("Index")); } var path = Path.GetTempFileName(); try { excelFile.SaveAs(path); var plc = _db.GetPLC(plcId.Value); var excelPositionList = new ExcelPositionList(path, plc.Id); if (!excelPositionList.LoadMessagesSheet()) { ViewBag.ErrorMessage = "Ошибка загрузки из Excel"; ViewBag.PlcFullName = plc.FullName; ViewBag.PlcId = plc.Id; return(View("Messages", plc.Messages)); } var dbMessages = plc.Messages.ToList();//.ToDictionary(x=>x.Number,y=>y); var excelMessages = excelPositionList.PlcMessages.Values; var merge = new PlcMessagesMerge(dbMessages, excelMessages, plc.Id); Session["PlcMessagesMerge"] = merge; ViewBag.PlcId = plc.Id; ViewBag.PlcFullName = plc.FullName; var diff = merge.Diff; return(View(diff)); } catch (Exception) { throw; } finally { System.IO.File.Delete(path); } }