70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
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();
|
|
}
|
|
}
|
|
|