public void CreateCSVFile(List <TransactionsList> list, string saveDir) { int index = 1; int percentage = 0; foreach (var item in list) { string savePath = saveDir + "\\" + item.Name + ".csv"; percentage = TransactionsHelper.GetPercentage(index, list.Count); Console.WriteLine("INFO: Creating File " + index + " of " + list.Count + "(" + percentage + "% Completed)\r"); using (StreamWriter sw = new StreamWriter(savePath)) { sw.WriteLine(String.Join(Settings.GetSettings.Delimiter, headers)); foreach (var entity in item.TransactionEntity) { string line = String.Join(Settings.GetSettings.Delimiter, new string[] { entity.TransactionDate.ToShortDateString(), entity.TransactionType, entity.TransactionDescription, entity.TransactionAmount.ToString(), entity.TransactionBalance.ToString() }); sw.WriteLine(line); } } index++; } }
private void CreateExcelDocument(List <TransactionsList> list, ExcelModel excelModel, string saveDir) { string savePath = string.Empty; string format = "yyyy/MM/dd"; int percentage = 0; int index = 1; foreach (var sheet in list) { using (ExcelPackage excelPackage = new ExcelPackage()) { savePath = saveDir + "\\" + sheet.Name + ".xlsx"; percentage = TransactionsHelper.GetPercentage(index, list.Count); string message = "INFO: Creating File " + index + " of " + list.Count + " (" + percentage + "% Completed)"; ConsoleHelper.Write(message); foreach (var entity in sheet.TransactionEntity) { char delimiter = Settings.GetSettings.Delimiter; string text = entity.TransactionDate.ToString(format) + delimiter + entity.TransactionType + delimiter + entity.TransactionDescription + delimiter + entity.TransactionAmount + delimiter + entity.TransactionBalance; ExcelTextFormat excelTextFormat = new ExcelTextFormat(); excelTextFormat.Delimiter = Settings.GetSettings.Delimiter; int row = 1; string name = "Other" + sheet.Name; Sheets excelSheet = excelModel.Sheets.Where(p => p.Items.Any(entity.TransactionType.ToLower().Contains) || p.Items.Any(entity.TransactionDescription.ToLower().Contains)).FirstOrDefault(); if (excelSheet != null) { if (excelPackage.Workbook.Worksheets[excelSheet.Name] == null) { excelPackage.Workbook.Worksheets.Add(excelSheet.Name); } name = excelSheet.Name; } else { if (excelPackage.Workbook.Worksheets[name] == null) { excelPackage.Workbook.Worksheets.Add(name); } } var worksheet = excelPackage.Workbook.Worksheets[name]; if (worksheet.Dimension == null) { row = 1; } else { row = worksheet.Dimension.End.Row + 1; } worksheet.Cells[row, 1, row, headers.Length].LoadFromText(text, excelTextFormat); worksheet.Cells[row, 1].Style.Numberformat.Format = format; } FileInfo excelFile = new FileInfo(savePath); excelPackage.SaveAs(excelFile); index++; } } Console.WriteLine("INFO: " + percentage + "% Completed, Saved file(s) to directory "); }
public async Task <List <TransactionsList> > ReadFileAsync(string filePath) { string[] data = new string[] { }; int lineCount = 0; string messages = string.Empty; if (File.Exists(filePath)) { using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8)) { string line = await sr.ReadLineAsync(); Console.WriteLine("INFO: ============== Reading File =============="); Console.WriteLine("INFO: Delimiter = " + Settings.GetSettings.Delimiter); List <TransactionsList> transactionsList = new List <TransactionsList>(); string[] lines = await File.ReadAllLinesAsync(filePath); lineCount = lines.Count(); int index = 1; while ((line = await sr.ReadLineAsync()) != null) { index++; int percentage = TransactionsHelper.GetPercentage(index, lineCount); ConsoleHelper.Write("INFO: Reading Line " + index + " of " + lineCount + " (" + percentage + "% completed)"); data = line.Split(Settings.GetSettings.Delimiter, StringSplitOptions.RemoveEmptyEntries); if (data.Length >= 5) { TransactionEntity transactionEntity = new TransactionEntity(); DateTime transactionDate = new DateTime(); transactionEntity.TransactionDate = data[0].Clean().ConvertStringToDate(); transactionEntity.TransactionType = data[1].Clean(); transactionEntity.TransactionDescription = data[2].Clean(); transactionEntity.TransactionAmount = data[3].ConvertToDecimal(); transactionEntity.TransactionBalance = data[4].ConvertToDecimal(); if (transactionEntity.TransactionDate != DateTime.MinValue) { transactionDate = transactionEntity.TransactionDate; string sheetName = transactionDate.ToString("MMM-yyyy"); TransactionsList findExisting = transactionsList.Find(t => t.Name == sheetName); if (findExisting != null) { findExisting.TransactionEntity.Add(transactionEntity); } else { TransactionsList newMonthlyTransaction = new TransactionsList(); newMonthlyTransaction.Name = sheetName; newMonthlyTransaction.Date = transactionDate; newMonthlyTransaction.TransactionEntity.Add(transactionEntity); transactionsList.Add(newMonthlyTransaction); } } else { messages += "WARNING:Line " + index + " can't parse date and time\n"; } } else { messages += "WARNING:Line " + index + " skipped invalid or empty data"; if (index != lineCount) { messages += "\n"; } } } Console.WriteLine(); Console.WriteLine(messages); return(transactionsList); } } else { Console.WriteLine("FATAL ERROR: Can't find file " + filePath); } return(null); }