diff --git a/MyYearlyCountings/Data/DataContext.cs b/MyYearlyCountings/Data/DataContext.cs index 1a5b8b5..a59f929 100644 --- a/MyYearlyCountings/Data/DataContext.cs +++ b/MyYearlyCountings/Data/DataContext.cs @@ -22,5 +22,9 @@ public class DataContext : DbContext public DbSet Members { get; set; } public DbSet AccountRecords { get; set; } + public DbSet CalYears { get; set; } + public DbSet CalMonths { get; set; } + public DbSet CalDays { get; set; } + public DbSet CalHours { get; set; } } diff --git a/MyYearlyCountings/Facades/IReadingIn.cs b/MyYearlyCountings/Facades/IReadingIn.cs index eed3b07..4e19e72 100644 --- a/MyYearlyCountings/Facades/IReadingIn.cs +++ b/MyYearlyCountings/Facades/IReadingIn.cs @@ -3,6 +3,6 @@ public interface IReadingIn { bool ReadAndSaveInvoices(string fullFileName); - IEnumerable ReadExcelInvoices(string fullFileName); + IEnumerable readXLS(string FilePath); } } \ No newline at end of file diff --git a/MyYearlyCountings/Facades/ReadingIn.cs b/MyYearlyCountings/Facades/ReadingIn.cs index 168853e..57cf467 100644 --- a/MyYearlyCountings/Facades/ReadingIn.cs +++ b/MyYearlyCountings/Facades/ReadingIn.cs @@ -2,8 +2,9 @@ using Microsoft.Extensions.Logging; using MyYearlyCountings.Helpers; using MyYearlyCountings.Repositories; +using OfficeOpenXml; using System.Runtime.InteropServices; -using Excel = Microsoft.Office.Interop.Excel; +//using Excel = Microsoft.Office.Interop.Excel; namespace MyYearlyCountings.Facades; @@ -25,78 +26,69 @@ public class ReadingIn : IReadingIn } // @"C:\dev\MyYearlyCountings\TransactionsTest.xls" - public IEnumerable ReadExcelInvoices(string fullFileName) + + public IEnumerable readXLS(string FilePath) { List records = new List(); AccountRecord? record = null; - - Excel.Application xlApp = new Excel.Application(); - Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fullFileName); - Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; - Excel.Range xlRange = xlWorksheet.UsedRange; - - var rowCount = xlRange.Rows.Count; - var colCount = xlRange.Columns.Count; - var prt = false; - - - //iterate over the rows and columns and print to the console as it appears in the file - //excel is not zero based!! - for (int i = 1; i <= rowCount; i++) + FileInfo existingFile = new FileInfo(FilePath); + using (ExcelPackage package = new ExcelPackage(existingFile)) { - for (int j = 1; j <= colCount; j++) + //get the first worksheet in the workbook + ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; + int colCount = worksheet.Dimension.End.Column; //get Column Count + int rowCount = worksheet.Dimension.End.Row; //get row count + bool prt = false; + for (int row = 1; row <= rowCount; row++) { - //new line - if (j == 1) + if (prt) { - if (prt) - { - Console.Write("\r\n"); - records.Add(record); - } - prt = false; - + Console.WriteLine(); + records.Add(record); } - //write the value to the console - if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) + prt = false; + for (int col = 1; col <= colCount; col++) { - string xx = xlRange.Cells[i, j].Value2.ToString(); - if ((j == 1) && xx.IsNumeric()) + if (worksheet.Cells[row, col].Value == null) { - DateTime dt = DateTime.FromOADate(xlRange.Cells[i, j].Value2); - prt = true; - if (prt) - { - Console.Write(dt.ToShortDateString() + "\t"); - record = new AccountRecord(); - record.BetalDatum = dt; - } + // Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null"); } else { + if (col == 1 && worksheet.Cells[row, col].Value.ToString().IsDate()) + { + prt = true; + record = new AccountRecord(); + } + //Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim()); if (prt) { - Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t"); - switch (j) + Console.Write($"{worksheet.Cells[row, col].Value.ToString().Trim()}/t"); + switch (col) { + case 1: + { + record.BetalDatum = DateTime.Parse( worksheet.Cells[row, col].Value.ToString().Trim()); + break; + } case 3: { - record.Mottagare = xlRange.Cells[i, j].Value2.ToString(); + record.Mottagare = worksheet.Cells[row, col].Value.ToString().Trim(); break; } case 5: { - record.Konto = xlRange.Cells[i, j].Value2.ToString(); + record.Konto = worksheet.Cells[row, col].Value.ToString().Trim(); break; } case 7: { - record.Belopp = xlRange.Cells[i, j].Value2; + record.Belopp = double.Parse(worksheet.Cells[row, col].Value.ToString().Trim()); break; } case 9: { - record.Avisering = xlRange.Cells[i, j].Value2.ToString(); + record.Avisering = worksheet.Cells[row, col].Value.ToString().Trim(); break; } @@ -104,47 +96,25 @@ public class ReadingIn : IReadingIn } } } - //add useful things here! } } - - //cleanup - GC.Collect(); - GC.WaitForPendingFinalizers(); - - //rule of thumb for releasing com objects: - // never use two dots, all COM objects must be referenced and released individually - // ex: [somthing].[something].[something] is bad - - //release com objects to fully kill excel process from running in the background - Marshal.ReleaseComObject(xlRange); - Marshal.ReleaseComObject(xlWorksheet); - - //close and release - xlWorkbook.Close(); - Marshal.ReleaseComObject(xlWorkbook); - - //quit and release - xlApp.Quit(); - Marshal.ReleaseComObject(xlApp); - return records; } - public bool ReadAndSaveInvoices(string fullFileName) { var result = true; - var restab = ReadExcelInvoices(fullFileName); + var restab = readXLS(fullFileName); if (restab != null) { try { - restab.ToList().ForEach(x => { - _accountRecordRepository.AddAccountRecord(x); + restab.ToList().ForEach(x => + { + _accountRecordRepository.AddAccountRecord(x); }); -// restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); }); + // restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); }); } catch (Exception ex) { diff --git a/MyYearlyCountings/Helpers/Extensions.cs b/MyYearlyCountings/Helpers/Extensions.cs index b3b984f..816c0c1 100644 --- a/MyYearlyCountings/Helpers/Extensions.cs +++ b/MyYearlyCountings/Helpers/Extensions.cs @@ -11,4 +11,16 @@ public static class Extensions } else return false; } + + public static bool IsDate(this string value) { + if (!string.IsNullOrEmpty(value) && value.Length<20) + { + if (DateTime.TryParse(value, out DateTime date)) + { + return true; + } + else return false; + } + else return false; + } } diff --git a/MyYearlyCountings/Models/CalDay.cs b/MyYearlyCountings/Models/CalDay.cs new file mode 100644 index 0000000..e2b5e7e --- /dev/null +++ b/MyYearlyCountings/Models/CalDay.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyYearlyCountings.Models +{ + public class CalDay + { + public int Id { get; set; } + public int Year { get; set; } + public int Month { get; set; } + public int Day { get; set; } + public string DayName { get; set; } = string.Empty; + public string DayComment { get; set; } = string.Empty; + } +} diff --git a/MyYearlyCountings/Models/CalHour.cs b/MyYearlyCountings/Models/CalHour.cs new file mode 100644 index 0000000..9688486 --- /dev/null +++ b/MyYearlyCountings/Models/CalHour.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyYearlyCountings.Models +{ + public class CalHour + { + public int Id { get; set; } + public int Year { get; set; } + public int Month { get; set; } + public int Day { get; set; } + public int Hour { get; set; } + public string HourComment { get; set; } = string.Empty; + } +} diff --git a/MyYearlyCountings/Models/CalMonth.cs b/MyYearlyCountings/Models/CalMonth.cs new file mode 100644 index 0000000..ce9f617 --- /dev/null +++ b/MyYearlyCountings/Models/CalMonth.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyYearlyCountings.Models +{ + public class CalMonth + { + public int Id { get; set; } + public int Year { get; set; } + public int Month { get; set; } + public string MonthName { get; set; } = string.Empty; + public string MonthComment { get; set; } = string.Empty; + } +} diff --git a/MyYearlyCountings/Models/CalYear.cs b/MyYearlyCountings/Models/CalYear.cs new file mode 100644 index 0000000..6812d25 --- /dev/null +++ b/MyYearlyCountings/Models/CalYear.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyYearlyCountings.Models +{ + public class CalYear + { + public int Id { get; set; } + public int Year { get; set; } + public string YearComment { get; set; } = string.Empty; + + } +} diff --git a/MyYearlyCountings/MyYearlyCountings.csproj b/MyYearlyCountings/MyYearlyCountings.csproj index 6687585..8af53a9 100644 --- a/MyYearlyCountings/MyYearlyCountings.csproj +++ b/MyYearlyCountings/MyYearlyCountings.csproj @@ -10,18 +10,7 @@ - - tlbimp - 9 - 1 - 00020813-0000-0000-c000-000000000046 - 0 - false - true - - - - + @@ -47,7 +36,7 @@ Always - Always + PreserveNewest diff --git a/MyYearlyCountings/Program.cs b/MyYearlyCountings/Program.cs index 599bf1e..a8320b0 100644 --- a/MyYearlyCountings/Program.cs +++ b/MyYearlyCountings/Program.cs @@ -8,6 +8,9 @@ using Microsoft.Extensions.Hosting; using MyYearlyCountings.Repositories; using MyYearlyCountings.Data; using MyYearlyCountings.Facades; +using OfficeOpenXml; + +ExcelPackage.LicenseContext = LicenseContext.NonCommercial; IHost host = CreateHostBuilder(args).Build(); var worker = ActivatorUtilities.CreateInstance((IServiceProvider)host.Services); diff --git a/MyYearlyCountings/UI/KeyHandling.cs b/MyYearlyCountings/UI/KeyHandling.cs index 7144894..6e4dfc7 100644 --- a/MyYearlyCountings/UI/KeyHandling.cs +++ b/MyYearlyCountings/UI/KeyHandling.cs @@ -6,18 +6,17 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MyYearlyCountings.UI +namespace MyYearlyCountings.UI; + +public class KeyHandling { - public class KeyHandling + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + + public KeyHandling(IConfiguration configuration, ILogger logger) { - private readonly IConfiguration _configuration; - private readonly ILogger _logger; - - public KeyHandling(IConfiguration configuration, ILogger logger) - { - _configuration = configuration; - _logger = logger; - } - + _configuration = configuration; + _logger = logger; } + } diff --git a/MyYearlyCountings/Worker.cs b/MyYearlyCountings/Worker.cs index 7811122..631693d 100644 --- a/MyYearlyCountings/Worker.cs +++ b/MyYearlyCountings/Worker.cs @@ -41,12 +41,18 @@ public class Worker { if (_configuration["UI"] == "XLS") { - if (!_readingIn.ReadAndSaveInvoices(@"C:\dev\MyYearlyCountings\TransactionsTest.xls")) + if (!_readingIn.ReadAndSaveInvoices(@"C:\dev\MyYearlyCountings\TransactionsTest.xlsx")) { - var resUlt = _readingIn.ReadExcelInvoices(@"C:\dev\MyYearlyCountings\TransactionsTest.xls"); + var resUlt = _readingIn.readXLS(@"C:\dev\MyYearlyCountings\TransactionsTest.xlsx"); resUlt.ToList().ForEach(rec => _logger.LogInformation($"Konto :{rec.Konto}, {rec.Belopp}")); } } + else if(_configuration["UI"] == "XLSNEW") + { + var records = _readingIn.readXLS(@"C:\dev\MyYearlyCountings\TransactionsTest.xlsx"); + records.ToList().ForEach(rec => _logger.LogInformation($"Konto :{rec.Konto}, {rec.Belopp}")); + records.ToList().ForEach(rec => _accountRecordRepository.AddAccountRecord(rec)); + } else { var ar = new AccountRecord { Konto = "BG 0867-4533", Mottagare = "NoOne", Belopp = 1270.34, BetalDatum = DateTime.Now.AddDays(10), Avisering = "Efaktura" }; diff --git a/MyYearlyCountings/appsettings.Development.json b/MyYearlyCountings/appsettings.Development.json index c552cd8..8826c54 100644 --- a/MyYearlyCountings/appsettings.Development.json +++ b/MyYearlyCountings/appsettings.Development.json @@ -1,4 +1,9 @@ { + "EPPlus": { + "ExcelPackage": { + "LicenseContext": "Polyform NonCommercial" //The license context used + } + }, "ConnectionStrings": { "DefaultConnection": "Data Source=.\\Local.db" } diff --git a/TransactionsTest.xlsx b/TransactionsTest.xlsx new file mode 100644 index 0000000..9409c97 Binary files /dev/null and b/TransactionsTest.xlsx differ