diff --git a/WinFormDi.BLI/IExcel.cs b/WinFormDi.BLI/IExcel.cs new file mode 100644 index 0000000..7651f1b --- /dev/null +++ b/WinFormDi.BLI/IExcel.cs @@ -0,0 +1,12 @@ +namespace WinFormDiApp.BLR +{ + public interface IExcel + { + int Columns { get; set; } + string DataType { get; set; } + int Rows { get; set; } + + void Dispose(); + string ReadCell(int i, int j); + } +} \ No newline at end of file diff --git a/WinFormDi.BLR/Excel.cs b/WinFormDi.BLR/Excel.cs new file mode 100644 index 0000000..e21ac58 --- /dev/null +++ b/WinFormDi.BLR/Excel.cs @@ -0,0 +1,57 @@ +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using _Excel = Microsoft.Office.Interop.Excel; + +namespace WinFormDiApp.BLR; + +public class Excel : IDisposable, IExcel +{ + string path = string.Empty; + _Application excel = new Application(); + Workbook wb; + Worksheet ws; + + public int Columns { get; set; } + public int Rows { get; set; } + + public string DataType { get; set; } + + public Excel(string path, int Sheet) + { + this.path = path; + wb = excel.Workbooks.Open(path); + ws = wb.Worksheets[Sheet]; + _Excel.Range usedRange = ws.UsedRange; + Columns = usedRange.Columns.Count; + Rows = usedRange.Rows.Count; + } + + public string ReadCell(int i, int j) + { + i++; + j++; + if (ws.Cells[i, j].Value2 != null) + { + _Excel.Range cell = ws.Cells[i, j]; + object value = cell.get_Value(Type.Missing); + if (value != null) + { + Type type = value.GetType(); + DataType = type.Name; + //Console.WriteLine($"Cell ({i},{j}) contains value of type {DataType}"); + } + return Convert.ToString(ws.Cells[i, j].Value2); + } + return string.Empty; + } + + public void Dispose() + { + excel.Workbooks.Close(); + } +} + diff --git a/WinFormDi.BLR/ReadingIn.cs b/WinFormDi.BLR/ReadingIn.cs index ba05346..0ca604b 100644 --- a/WinFormDi.BLR/ReadingIn.cs +++ b/WinFormDi.BLR/ReadingIn.cs @@ -2,11 +2,8 @@ using Microsoft.Extensions.Logging; using WinFormDiApp.BL.Helpers; using WinFormDiApp.BLI; -using OfficeOpenXml; -using System.Runtime.InteropServices; using WinFormDiApp.BL.Models; -//using Excel = Microsoft.Office.Interop.Excel; - +using Microsoft.Extensions.DependencyInjection; namespace WinFormDiApp.BLR; @@ -15,7 +12,7 @@ public class ReadingIn : IReadingIn private readonly IConfiguration _configuration; private readonly ILogger _logger; private readonly IAccountRecordRepository _accountRecordRepository; - + public ReadingIn( IConfiguration configuration, ILogger logger, @@ -33,31 +30,32 @@ public class ReadingIn : IReadingIn List records = new List(); AccountRecord? record = null; FileInfo existingFile = new FileInfo(FilePath); - using (ExcelPackage package = new ExcelPackage(existingFile)) + using (Excel exObject = new Excel(existingFile.FullName, 1)) { //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 + int colCount = exObject.Columns; + int rowCount = exObject.Rows; //get row count bool prt = false; - for (int row = 1; row <= rowCount; row++) + for (int row = 0; row <= rowCount; row++) { if (prt) { //Console.WriteLine(); - _logger.LogInformation(""); + //// _logger.LogInformation(""); records.Add(record); } prt = false; - for (int col = 1; col <= colCount; col++) + for (int col = 0; col <= colCount; col++) { - if (worksheet.Cells[row, col].Value == null) + var x = exObject.ReadCell(row, col); + + if (exObject.ReadCell(row, col) == null) { // Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null"); } else { - if (col == 1 && worksheet.Cells[row, col].Value.ToString().IsDate()) + if (col == 0 && exObject.DataType == "DateTime") { prt = true; record = new AccountRecord(); @@ -65,32 +63,32 @@ public class ReadingIn : IReadingIn //Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim()); if (prt) { - _logger.LogInformation($"{worksheet.Cells[row, col].Value.ToString().Trim()}/t"); + // _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t"); switch (col) { - case 1: + case 0: { - record.BetalDatum = DateTime.Parse( worksheet.Cells[row, col].Value.ToString().Trim()); + record.BetalDatum = DateTime.FromOADate(double.Parse(exObject.ReadCell(row, col))); break; } - case 3: + case 2: { - record.Mottagare = worksheet.Cells[row, col].Value.ToString().Trim(); + record.Mottagare = exObject.ReadCell(row, col).ToString().Trim(); break; } - case 5: + case 4: { - record.Konto = worksheet.Cells[row, col].Value.ToString().Trim(); + record.Konto = exObject.ReadCell(row, col).ToString().Trim(); break; } - case 7: + case 6: { - record.Belopp = double.Parse(worksheet.Cells[row, col].Value.ToString().Trim()); + record.Belopp = double.Parse(exObject.ReadCell(row, col).ToString().Trim()); break; } - case 9: + case 8: { - record.Avisering = worksheet.Cells[row, col].Value.ToString().Trim(); + record.Avisering = exObject.ReadCell(row, col).ToString().Trim(); break; } diff --git a/WinFormDi.BLR/WinFormDiApp.BLR.csproj b/WinFormDi.BLR/WinFormDiApp.BLR.csproj index f30997f..a66f6ce 100644 --- a/WinFormDi.BLR/WinFormDiApp.BLR.csproj +++ b/WinFormDi.BLR/WinFormDiApp.BLR.csproj @@ -1,4 +1,4 @@ - + net7.0 @@ -6,14 +6,22 @@ enable + + + tlbimp + 9 + 1 + 00020813-0000-0000-c000-000000000046 + 0 + false + true + + + - - - - diff --git a/WinFormDi/ContainerConfig.cs b/WinFormDi/ContainerConfig.cs index 0851846..194a5ef 100644 --- a/WinFormDi/ContainerConfig.cs +++ b/WinFormDi/ContainerConfig.cs @@ -40,6 +40,7 @@ namespace WinFormDiApp .AddTransient() .AddTransient() .AddTransient() + .AddTransient() .AddTransient() .AddTransient() .AddTransient();