Changed reading strategy, uses interop instead of EPPlus
This commit is contained in:
@ -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<ReadingIn> _logger;
|
||||
private readonly IAccountRecordRepository _accountRecordRepository;
|
||||
|
||||
|
||||
public ReadingIn(
|
||||
IConfiguration configuration,
|
||||
ILogger<ReadingIn> logger,
|
||||
@ -33,31 +30,32 @@ public class ReadingIn : IReadingIn
|
||||
List<AccountRecord> records = new List<AccountRecord>();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user