Changed reading strategy, uses interop instead of EPPlus

This commit is contained in:
2023-08-31 08:33:26 +02:00
parent aef30274b2
commit f56a4bca3e
5 changed files with 106 additions and 30 deletions

57
WinFormDi.BLR/Excel.cs Normal file
View File

@ -0,0 +1,57 @@
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 Excel : IDisposable, IExcel
{
string path = string.Empty;
_Application excel = new Application();
Workbook wb;
Worksheet ws;
public int Columns { get; set; }
public int Rows { get; set; }
public string DataType { get; set; }
public Excel(string path, int Sheet)
{
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();
}
}