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

@ -0,0 +1,69 @@
using Microsoft.Extensions.Logging;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _Excel = Microsoft.Office.Interop.Excel;
namespace WinFormDiApp.BLR;
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 void ExcellentStart(string path, int Sheet)
{
if (wb != null)
{
wb.Close();
}
this.path = path;
wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[Sheet];
_Excel.Range usedRange = ws.UsedRange;
Columns = usedRange.Columns.Count;
Rows = usedRange.Rows.Count;
}
public string ReadCell(int i, int j)
{
i++;
j++;
if (ws.Cells[i, j].Value2 != null)
{
_Excel.Range cell = ws.Cells[i, j];
object value = cell.get_Value(Type.Missing);
if (value != null)
{
Type type = value.GetType();
DataType = type.Name;
//Console.WriteLine($"Cell ({i},{j}) contains value of type {DataType}");
}
return Convert.ToString(ws.Cells[i, j].Value2);
}
return string.Empty;
}
public void Dispose()
{
excel.Workbooks.Close();
}
}