Now it works reading in different transactionsheets from excel
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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];
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -40,7 +40,7 @@ namespace WinFormDiApp
|
||||
.AddTransient<IAccountRecordRepository,AccountRecordRepository>()
|
||||
.AddTransient<IMemberRepository,MemberRepository>()
|
||||
.AddTransient<IReadingIn, ReadingIn>()
|
||||
.AddTransient<IExcel, Excel>()
|
||||
.AddTransient<IExcellent, Excellent>()
|
||||
.AddTransient<MainWindow>()
|
||||
.AddTransient<frmReadPayments>()
|
||||
.AddTransient<frmPayments>();
|
||||
|
||||
@ -18,6 +18,7 @@ namespace WinFormDiApp
|
||||
public frmPayments(IAccountRecordRepository accountRecordRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
lvPayments.Items.Clear();
|
||||
_accountRecordRepository = accountRecordRepository;
|
||||
}
|
||||
|
||||
|
||||
1
WinFormDi/frmReadPayments.Designer.cs
generated
1
WinFormDi/frmReadPayments.Designer.cs
generated
@ -192,7 +192,6 @@
|
||||
Name = "frmReadPayments";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
Text = "frmReadPayments";
|
||||
Load += frmReadPayments_Load;
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ namespace WinFormDiApp
|
||||
public frmReadPayments(IAccountRecordRepository accountRecordRepository, IReadingIn readingIn, ILogger<frmReadPayments> 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)";
|
||||
|
||||
Reference in New Issue
Block a user