Now it works reading in different transactionsheets from excel

This commit is contained in:
2023-08-31 22:37:48 +02:00
parent f56a4bca3e
commit 2b4d10070f
7 changed files with 73 additions and 62 deletions

View File

@ -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<Excellent> logger)
{
_logger = logger;
}
string path = string.Empty;
_Application excel = new Application();
Workbook wb;
Worksheet ws;
private readonly ILogger<Excellent> _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];

View File

@ -12,15 +12,18 @@ public class ReadingIn : IReadingIn
private readonly IConfiguration _configuration;
private readonly ILogger<ReadingIn> _logger;
private readonly IAccountRecordRepository _accountRecordRepository;
private readonly IExcellent _excellent;
public ReadingIn(
IConfiguration configuration,
ILogger<ReadingIn> 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<AccountRecord> records = new List<AccountRecord>();
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;
}