public static string GetPreview(string lines, ReplaceSettings replace = null, string nameOfFile = null) { if (replace != null) { string retVal = ""; string aLine = null; StringReader strReader = new StringReader(lines); while ((aLine = strReader.ReadLine()) != null) { retVal += replace.Replace(aLine, nameOfFile) + "\n"; } return(retVal); } else { return(lines); } }
private void TotalsControls1_OnParse(ProgressBar progressBar) { var progress = 100 / (decimal)ParseSettings.Count; var totalProgress = 0m; var tempData = new List <TotalsModel>(); foreach (var file in ParseSettings) { if (!File.Exists(file.FilePath)) { continue; } var stream = File.OpenRead(file.FilePath); IEnumerable <TotalsParseModel> parsedData = new List <TotalsParseModel>(); try { parsedData = stream.Parse <TotalsParseModel>(new { file.ItemNo, file.Quantity }); } catch (Exception e) { MessageBox.Show($"Ошибка парсинга файла!\r\nЗакройте Excell если он открыт и повторите попытку.\r\nСейчас файл будет пропущен.\r\n{e.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } var data = parsedData.Select(x => new TotalsModel { ItemNo = x.ItemNo, City = file.City, StoreType = file.StoreType, Quantity = x.Quantity }).ToList(); tempData.AddRange(data); totalProgress += progress; progressBar.Value = Convert.ToInt32(totalProgress); } MessageBox.Show("Готово", "Парсинг завершен", MessageBoxButtons.OK, MessageBoxIcon.Information); if (!tempData.Any()) { DataList = DataListOrig = new List <TotalsModel>(); return; } //tempData = tempData.Join(ReplaceSettings, x => x.ItemNo, x => x.ReplaceNo, // (l, r) => new TotalsModel() // { // ItemNo = r.ItemNo, // StoreType = l.StoreType, // City = l.City, // Quantity = l.Quantity // }) tempData = tempData.Select(x => { var replace = ReplaceSettings.FirstOrDefault(y => y.ReplaceNo.Equals(x.ItemNo, StringComparison.CurrentCultureIgnoreCase)); if (replace != null) { x.ItemNo = replace.ItemNo; } return(x); }) .GroupBy(x => new { x.ItemNo, x.City, x.StoreType }, (x, model) => new TotalsModel() { ItemNo = x.ItemNo, StoreType = x.StoreType, City = x.City, Quantity = model.Sum(y => y.Quantity) }).ToList(); if (!tempData.Any()) { DataList = DataListOrig = new List <TotalsModel>(); return; } tempData = tempData.Distinct(new TotalsParseModelEqualityComparer()).ToList(); if (!tempData.Any()) { DataList = DataListOrig = new List <TotalsModel>(); } else { DataList = DataListOrig = tempData; } RefreshTableBinding(); }
public static bool CreateFile(string nameOfFile, string extention, string lines, ReplaceSettings replaceSettings) { bool retVal = false; string directory = GetProjectDirectory(); if (directory != null) { string path = directory + "\\" + nameOfFile + extention; if (PathValidCheck(path)) { // Create a new file StreamWriter sw = File.CreateText(path); if (lines != null && lines != "") { string aLine = null; StringReader strReader = new StringReader(lines); if (replaceSettings != null) { while ((aLine = strReader.ReadLine()) != null) { sw.WriteLine(replaceSettings.Replace(aLine, nameOfFile)); retVal = true; } } else { while ((aLine = strReader.ReadLine()) != null) { sw.WriteLine(aLine); retVal = true; } } } sw.Close(); } } return(retVal); }