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,12 +1,13 @@
namespace WinFormDiApp.BLR namespace WinFormDiApp.BLR
{ {
public interface IExcel public interface IExcellent
{ {
int Columns { get; set; } int Columns { get; set; }
string DataType { get; set; } string DataType { get; set; }
int Rows { get; set; } int Rows { get; set; }
void Dispose(); void Dispose();
void ExcellentStart(string path, int Sheet);
string ReadCell(int i, int j); string ReadCell(int i, int j);
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Extensions.Logging;
using Microsoft.Office.Interop.Excel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -8,20 +9,31 @@ using _Excel = Microsoft.Office.Interop.Excel;
namespace WinFormDiApp.BLR; namespace WinFormDiApp.BLR;
public class Excel : IDisposable, IExcel public class Excellent : IDisposable, IExcellent
{ {
public Excellent(ILogger<Excellent> logger)
{
_logger = logger;
}
string path = string.Empty; string path = string.Empty;
_Application excel = new Application(); _Application excel = new Application();
Workbook wb; Workbook wb;
Worksheet ws; Worksheet ws;
private readonly ILogger<Excellent> _logger;
public int Columns { get; set; } public int Columns { get; set; }
public int Rows { get; set; } public int Rows { get; set; }
public string DataType { 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; this.path = path;
wb = excel.Workbooks.Open(path); wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[Sheet]; ws = wb.Worksheets[Sheet];

View File

@ -12,15 +12,18 @@ public class ReadingIn : IReadingIn
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
private readonly ILogger<ReadingIn> _logger; private readonly ILogger<ReadingIn> _logger;
private readonly IAccountRecordRepository _accountRecordRepository; private readonly IAccountRecordRepository _accountRecordRepository;
private readonly IExcellent _excellent;
public ReadingIn( public ReadingIn(
IConfiguration configuration, IConfiguration configuration,
ILogger<ReadingIn> logger, ILogger<ReadingIn> logger,
IAccountRecordRepository accountRecordRepository) IAccountRecordRepository accountRecordRepository,
IExcellent excellent)
{ {
_configuration = configuration; _configuration = configuration;
_logger = logger; _logger = logger;
_accountRecordRepository = accountRecordRepository; _accountRecordRepository = accountRecordRepository;
_excellent = excellent;
} }
// @"C:\dev\MyYearlyCountings\TransactionsTest.xls" // @"C:\dev\MyYearlyCountings\TransactionsTest.xls"
@ -30,65 +33,73 @@ public class ReadingIn : IReadingIn
List<AccountRecord> records = new List<AccountRecord>(); List<AccountRecord> records = new List<AccountRecord>();
AccountRecord? record = null; AccountRecord? record = null;
FileInfo existingFile = new FileInfo(FilePath); FileInfo existingFile = new FileInfo(FilePath);
using (Excel exObject = new Excel(existingFile.FullName, 1)) var fieldNr = 0;
{
//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);
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") if (x == "") { }
{ else
prt = true;
record = new AccountRecord();
}
//Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
if (prt)
{ {
fieldNr++;
// _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t"); // _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; break;
} }
case 2: 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; break;
} }
case 4: case 4:
{ {
record.Konto = exObject.ReadCell(row, col).ToString().Trim(); record.Belopp = double.Parse(_excellent.ReadCell(row, col).ToString().Trim());
break; break;
} }
case 6: case 5:
{ {
record.Belopp = double.Parse(exObject.ReadCell(row, col).ToString().Trim()); record.Avisering = _excellent.ReadCell(row, col).ToString().Trim();
break;
}
case 8:
{
record.Avisering = exObject.ReadCell(row, col).ToString().Trim();
break; break;
} }

View File

@ -40,7 +40,7 @@ namespace WinFormDiApp
.AddTransient<IAccountRecordRepository,AccountRecordRepository>() .AddTransient<IAccountRecordRepository,AccountRecordRepository>()
.AddTransient<IMemberRepository,MemberRepository>() .AddTransient<IMemberRepository,MemberRepository>()
.AddTransient<IReadingIn, ReadingIn>() .AddTransient<IReadingIn, ReadingIn>()
.AddTransient<IExcel, Excel>() .AddTransient<IExcellent, Excellent>()
.AddTransient<MainWindow>() .AddTransient<MainWindow>()
.AddTransient<frmReadPayments>() .AddTransient<frmReadPayments>()
.AddTransient<frmPayments>(); .AddTransient<frmPayments>();

View File

@ -18,6 +18,7 @@ namespace WinFormDiApp
public frmPayments(IAccountRecordRepository accountRecordRepository) public frmPayments(IAccountRecordRepository accountRecordRepository)
{ {
InitializeComponent(); InitializeComponent();
lvPayments.Items.Clear();
_accountRecordRepository = accountRecordRepository; _accountRecordRepository = accountRecordRepository;
} }

View File

@ -192,7 +192,6 @@
Name = "frmReadPayments"; Name = "frmReadPayments";
StartPosition = FormStartPosition.CenterScreen; StartPosition = FormStartPosition.CenterScreen;
Text = "frmReadPayments"; Text = "frmReadPayments";
Load += frmReadPayments_Load;
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }

View File

@ -13,6 +13,7 @@ namespace WinFormDiApp
public frmReadPayments(IAccountRecordRepository accountRecordRepository, IReadingIn readingIn, ILogger<frmReadPayments> logger) public frmReadPayments(IAccountRecordRepository accountRecordRepository, IReadingIn readingIn, ILogger<frmReadPayments> logger)
{ {
InitializeComponent(); InitializeComponent();
lvPayouts.Items.Clear();
_accountRecordRepository = accountRecordRepository; _accountRecordRepository = accountRecordRepository;
_readingIn = readingIn; _readingIn = readingIn;
_logger = logger; _logger = logger;
@ -24,20 +25,6 @@ namespace WinFormDiApp
this.Close(); 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) private void btnChooseFile_Click(object sender, EventArgs e)
{ {
ofChooseFile.Title = "Välj nerladdad excel-fil (Transaktioner)"; ofChooseFile.Title = "Välj nerladdad excel-fil (Transaktioner)";