diff --git a/WinFormDi.BLI/IExcel.cs b/WinFormDi.BLI/IExcellent.cs similarity index 71% rename from WinFormDi.BLI/IExcel.cs rename to WinFormDi.BLI/IExcellent.cs index 7651f1b..d0d0621 100644 --- a/WinFormDi.BLI/IExcel.cs +++ b/WinFormDi.BLI/IExcellent.cs @@ -1,12 +1,13 @@ namespace WinFormDiApp.BLR { - public interface IExcel + public interface IExcellent { int Columns { get; set; } string DataType { get; set; } int Rows { get; set; } void Dispose(); + void ExcellentStart(string path, int Sheet); string ReadCell(int i, int j); } } \ No newline at end of file diff --git a/WinFormDi.BLR/Excel.cs b/WinFormDi.BLR/Excellent.cs similarity index 77% rename from WinFormDi.BLR/Excel.cs rename to WinFormDi.BLR/Excellent.cs index e21ac58..da9c791 100644 --- a/WinFormDi.BLR/Excel.cs +++ b/WinFormDi.BLR/Excellent.cs @@ -1,4 +1,5 @@ -using Microsoft.Office.Interop.Excel; +using Microsoft.Extensions.Logging; +using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Linq; @@ -8,20 +9,31 @@ using _Excel = Microsoft.Office.Interop.Excel; namespace WinFormDiApp.BLR; -public class Excel : IDisposable, IExcel +public class Excellent : IDisposable, IExcellent { + + public Excellent(ILogger logger) + { + _logger = logger; + } + string path = string.Empty; _Application excel = new Application(); Workbook wb; Worksheet ws; + private readonly ILogger _logger; public int Columns { get; set; } public int Rows { get; set; } public string DataType { get; set; } - public Excel(string path, int Sheet) + public void ExcellentStart(string path, int Sheet) { + if (wb != null) + { + wb.Close(); + } this.path = path; wb = excel.Workbooks.Open(path); ws = wb.Worksheets[Sheet]; diff --git a/WinFormDi.BLR/ReadingIn.cs b/WinFormDi.BLR/ReadingIn.cs index 0ca604b..0764f92 100644 --- a/WinFormDi.BLR/ReadingIn.cs +++ b/WinFormDi.BLR/ReadingIn.cs @@ -12,15 +12,18 @@ public class ReadingIn : IReadingIn private readonly IConfiguration _configuration; private readonly ILogger _logger; private readonly IAccountRecordRepository _accountRecordRepository; - + private readonly IExcellent _excellent; + public ReadingIn( IConfiguration configuration, ILogger logger, - IAccountRecordRepository accountRecordRepository) + IAccountRecordRepository accountRecordRepository, + IExcellent excellent) { _configuration = configuration; _logger = logger; _accountRecordRepository = accountRecordRepository; + _excellent = excellent; } // @"C:\dev\MyYearlyCountings\TransactionsTest.xls" @@ -30,65 +33,73 @@ public class ReadingIn : IReadingIn List records = new List(); AccountRecord? record = null; FileInfo existingFile = new FileInfo(FilePath); - using (Excel exObject = new Excel(existingFile.FullName, 1)) - { - //get the first worksheet in the workbook - int colCount = exObject.Columns; - int rowCount = exObject.Rows; //get row count - bool prt = false; - for (int row = 0; row <= rowCount; row++) - { - if (prt) - { - //Console.WriteLine(); - //// _logger.LogInformation(""); - records.Add(record); - } - prt = false; - for (int col = 0; col <= colCount; col++) - { - var x = exObject.ReadCell(row, col); + var fieldNr = 0; - if (exObject.ReadCell(row, col) == null) + _excellent.ExcellentStart(existingFile.FullName, 1); ; + + //get the first worksheet in the workbook + int colCount = _excellent.Columns; + int rowCount = _excellent.Rows; //get row count + bool prt = false; + for (int row = 0; row <= rowCount; row++) + { + if (prt) + { + //Console.WriteLine(); + //// _logger.LogInformation(""); + records.Add(record); + fieldNr = 0; + } + prt = false; + for (int col = 0; col <= colCount; col++) + { + var x = _excellent.ReadCell(row, col); + + if (_excellent.ReadCell(row, col) == null) + { + // Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null"); + } + else + { + if (col == 0 && _excellent.DataType == "DateTime") { - // Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null"); + prt = true; + record = new AccountRecord(); } - else + //Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim()); + if (prt) { - if (col == 0 && exObject.DataType == "DateTime") - { - prt = true; - record = new AccountRecord(); - } - //Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim()); - if (prt) + if (x == "") { } + else { + fieldNr++; + // _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t"); - switch (col) + switch (fieldNr) { - case 0: + case 1: { - record.BetalDatum = DateTime.FromOADate(double.Parse(exObject.ReadCell(row, col))); + record.BetalDatum = DateTime.FromOADate(double.Parse(_excellent.ReadCell(row, col))); break; } case 2: { - record.Mottagare = exObject.ReadCell(row, col).ToString().Trim(); + record.Mottagare = _excellent.ReadCell(row, col).ToString().Trim(); + break; + } + case 3: + { + record.Konto = _excellent.ReadCell(row, col).ToString().Trim(); break; } case 4: { - record.Konto = exObject.ReadCell(row, col).ToString().Trim(); + record.Belopp = double.Parse(_excellent.ReadCell(row, col).ToString().Trim()); break; } - case 6: + case 5: { - record.Belopp = double.Parse(exObject.ReadCell(row, col).ToString().Trim()); - break; - } - case 8: - { - record.Avisering = exObject.ReadCell(row, col).ToString().Trim(); + record.Avisering = _excellent.ReadCell(row, col).ToString().Trim(); break; } diff --git a/WinFormDi/ContainerConfig.cs b/WinFormDi/ContainerConfig.cs index 194a5ef..1068567 100644 --- a/WinFormDi/ContainerConfig.cs +++ b/WinFormDi/ContainerConfig.cs @@ -40,7 +40,7 @@ namespace WinFormDiApp .AddTransient() .AddTransient() .AddTransient() - .AddTransient() + .AddTransient() .AddTransient() .AddTransient() .AddTransient(); diff --git a/WinFormDi/frmPayments.cs b/WinFormDi/frmPayments.cs index 49c659e..f8be8cb 100644 --- a/WinFormDi/frmPayments.cs +++ b/WinFormDi/frmPayments.cs @@ -18,6 +18,7 @@ namespace WinFormDiApp public frmPayments(IAccountRecordRepository accountRecordRepository) { InitializeComponent(); + lvPayments.Items.Clear(); _accountRecordRepository = accountRecordRepository; } diff --git a/WinFormDi/frmReadPayments.Designer.cs b/WinFormDi/frmReadPayments.Designer.cs index 885f8fa..c9502c2 100644 --- a/WinFormDi/frmReadPayments.Designer.cs +++ b/WinFormDi/frmReadPayments.Designer.cs @@ -192,7 +192,6 @@ Name = "frmReadPayments"; StartPosition = FormStartPosition.CenterScreen; Text = "frmReadPayments"; - Load += frmReadPayments_Load; ResumeLayout(false); PerformLayout(); } diff --git a/WinFormDi/frmReadPayments.cs b/WinFormDi/frmReadPayments.cs index a7da9ac..4151151 100644 --- a/WinFormDi/frmReadPayments.cs +++ b/WinFormDi/frmReadPayments.cs @@ -13,6 +13,7 @@ namespace WinFormDiApp public frmReadPayments(IAccountRecordRepository accountRecordRepository, IReadingIn readingIn, ILogger logger) { InitializeComponent(); + lvPayouts.Items.Clear(); _accountRecordRepository = accountRecordRepository; _readingIn = readingIn; _logger = logger; @@ -24,20 +25,6 @@ namespace WinFormDiApp this.Close(); } - private void frmReadPayments_Load(object sender, EventArgs e) - { - var payments = _accountRecordRepository.GetAllAccounts(); - foreach (var account in payments) - { - var lvitem = lvPayments.Items.Add(account.Id.ToString()); - lvitem.SubItems.Add(account.Mottagare); - lvitem.SubItems.Add(account.Konto); - lvitem.SubItems.Add(account.Belopp.ToString()); - lvitem.SubItems.Add(account.BetalDatum.ToShortDateString()); - lvitem.SubItems.Add(account.Avisering); - } - } - private void btnChooseFile_Click(object sender, EventArgs e) { ofChooseFile.Title = "Välj nerladdad excel-fil (Transaktioner)";