132 lines
4.9 KiB
C#
132 lines
4.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
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;
|
|
|
|
|
|
namespace WinFormDiApp.BLR;
|
|
|
|
public class ReadingIn : IReadingIn
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<ReadingIn> _logger;
|
|
private readonly IAccountRecordRepository _accountRecordRepository;
|
|
|
|
public ReadingIn(
|
|
IConfiguration configuration,
|
|
ILogger<ReadingIn> logger,
|
|
IAccountRecordRepository accountRecordRepository)
|
|
{
|
|
_configuration = configuration;
|
|
_logger = logger;
|
|
_accountRecordRepository = accountRecordRepository;
|
|
}
|
|
|
|
// @"C:\dev\MyYearlyCountings\TransactionsTest.xls"
|
|
|
|
public IEnumerable<AccountRecord> readXLS(string FilePath)
|
|
{
|
|
List<AccountRecord> records = new List<AccountRecord>();
|
|
AccountRecord? record = null;
|
|
FileInfo existingFile = new FileInfo(FilePath);
|
|
using (ExcelPackage package = new ExcelPackage(existingFile))
|
|
{
|
|
//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
|
|
bool prt = false;
|
|
for (int row = 1; row <= rowCount; row++)
|
|
{
|
|
if (prt)
|
|
{
|
|
//Console.WriteLine();
|
|
_logger.LogInformation("");
|
|
records.Add(record);
|
|
}
|
|
prt = false;
|
|
for (int col = 1; col <= colCount; col++)
|
|
{
|
|
if (worksheet.Cells[row, col].Value == null)
|
|
{
|
|
// Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null");
|
|
}
|
|
else
|
|
{
|
|
if (col == 1 && worksheet.Cells[row, col].Value.ToString().IsDate())
|
|
{
|
|
prt = true;
|
|
record = new AccountRecord();
|
|
}
|
|
//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");
|
|
switch (col)
|
|
{
|
|
case 1:
|
|
{
|
|
record.BetalDatum = DateTime.Parse( worksheet.Cells[row, col].Value.ToString().Trim());
|
|
break;
|
|
}
|
|
case 3:
|
|
{
|
|
record.Mottagare = worksheet.Cells[row, col].Value.ToString().Trim();
|
|
break;
|
|
}
|
|
case 5:
|
|
{
|
|
record.Konto = worksheet.Cells[row, col].Value.ToString().Trim();
|
|
break;
|
|
}
|
|
case 7:
|
|
{
|
|
record.Belopp = double.Parse(worksheet.Cells[row, col].Value.ToString().Trim());
|
|
break;
|
|
}
|
|
case 9:
|
|
{
|
|
record.Avisering = worksheet.Cells[row, col].Value.ToString().Trim();
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return records;
|
|
}
|
|
public bool ReadAndSaveInvoices(string fullFileName)
|
|
{
|
|
var result = true;
|
|
var restab = readXLS(fullFileName);
|
|
if (restab != null)
|
|
{
|
|
try
|
|
{
|
|
|
|
restab.ToList().ForEach(x =>
|
|
{
|
|
_accountRecordRepository.AddAccountRecord(x);
|
|
});
|
|
|
|
// restab.ToList().ForEach(x => { _accountRecordRepository.AddAccountRecord(x); });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"MassUppdatering misslyckat: {ex.Message}");
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|