Changed reading strategy, uses interop instead of EPPlus
This commit is contained in:
12
WinFormDi.BLI/IExcel.cs
Normal file
12
WinFormDi.BLI/IExcel.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace WinFormDiApp.BLR
|
||||
{
|
||||
public interface IExcel
|
||||
{
|
||||
int Columns { get; set; }
|
||||
string DataType { get; set; }
|
||||
int Rows { get; set; }
|
||||
|
||||
void Dispose();
|
||||
string ReadCell(int i, int j);
|
||||
}
|
||||
}
|
||||
57
WinFormDi.BLR/Excel.cs
Normal file
57
WinFormDi.BLR/Excel.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
@ -6,14 +6,22 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<COMReference Include="Microsoft.Office.Interop.Excel">
|
||||
<WrapperTool>tlbimp</WrapperTool>
|
||||
<VersionMinor>9</VersionMinor>
|
||||
<VersionMajor>1</VersionMajor>
|
||||
<Guid>00020813-0000-0000-c000-000000000046</Guid>
|
||||
<Lcid>0</Lcid>
|
||||
<Isolated>false</Isolated>
|
||||
<EmbedInteropTypes>true</EmbedInteropTypes>
|
||||
</COMReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WinFormDi.BLI\WinFormDiApp.BLI.csproj" />
|
||||
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
|
||||
<ProjectReference Include="..\WinFormDiApp.DAL\WinFormDiApp.DAL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="EPPlus" Version="6.2.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -40,6 +40,7 @@ namespace WinFormDiApp
|
||||
.AddTransient<IAccountRecordRepository,AccountRecordRepository>()
|
||||
.AddTransient<IMemberRepository,MemberRepository>()
|
||||
.AddTransient<IReadingIn, ReadingIn>()
|
||||
.AddTransient<IExcel, Excel>()
|
||||
.AddTransient<MainWindow>()
|
||||
.AddTransient<frmReadPayments>()
|
||||
.AddTransient<frmPayments>();
|
||||
|
||||
Reference in New Issue
Block a user