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 Microsoft.Extensions.Logging;
|
||||||
using WinFormDiApp.BL.Helpers;
|
using WinFormDiApp.BL.Helpers;
|
||||||
using WinFormDiApp.BLI;
|
using WinFormDiApp.BLI;
|
||||||
using OfficeOpenXml;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using WinFormDiApp.BL.Models;
|
using WinFormDiApp.BL.Models;
|
||||||
//using Excel = Microsoft.Office.Interop.Excel;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
|
||||||
namespace WinFormDiApp.BLR;
|
namespace WinFormDiApp.BLR;
|
||||||
|
|
||||||
@ -33,31 +30,32 @@ public class ReadingIn : IReadingIn
|
|||||||
List<AccountRecord> records = new List<AccountRecord>();
|
List<AccountRecord> records = new List<AccountRecord>();
|
||||||
AccountRecord? record = null;
|
AccountRecord? record = null;
|
||||||
FileInfo existingFile = new FileInfo(FilePath);
|
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
|
//get the first worksheet in the workbook
|
||||||
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
|
int colCount = exObject.Columns;
|
||||||
int colCount = worksheet.Dimension.End.Column; //get Column Count
|
int rowCount = exObject.Rows; //get row count
|
||||||
int rowCount = worksheet.Dimension.End.Row; //get row count
|
|
||||||
bool prt = false;
|
bool prt = false;
|
||||||
for (int row = 1; row <= rowCount; row++)
|
for (int row = 0; row <= rowCount; row++)
|
||||||
{
|
{
|
||||||
if (prt)
|
if (prt)
|
||||||
{
|
{
|
||||||
//Console.WriteLine();
|
//Console.WriteLine();
|
||||||
_logger.LogInformation("");
|
//// _logger.LogInformation("");
|
||||||
records.Add(record);
|
records.Add(record);
|
||||||
}
|
}
|
||||||
prt = false;
|
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");
|
// Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + "null");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (col == 1 && worksheet.Cells[row, col].Value.ToString().IsDate())
|
if (col == 0 && exObject.DataType == "DateTime")
|
||||||
{
|
{
|
||||||
prt = true;
|
prt = true;
|
||||||
record = new AccountRecord();
|
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());
|
//Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
|
||||||
if (prt)
|
if (prt)
|
||||||
{
|
{
|
||||||
_logger.LogInformation($"{worksheet.Cells[row, col].Value.ToString().Trim()}/t");
|
// _logger.LogInformation($"{exObject.ReadCell(row, col).ToString().Trim()}/t");
|
||||||
switch (col)
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case 3:
|
case 2:
|
||||||
{
|
{
|
||||||
record.Mottagare = worksheet.Cells[row, col].Value.ToString().Trim();
|
record.Mottagare = exObject.ReadCell(row, col).ToString().Trim();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 5:
|
case 4:
|
||||||
{
|
{
|
||||||
record.Konto = worksheet.Cells[row, col].Value.ToString().Trim();
|
record.Konto = exObject.ReadCell(row, col).ToString().Trim();
|
||||||
break;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case 9:
|
case 8:
|
||||||
{
|
{
|
||||||
record.Avisering = worksheet.Cells[row, col].Value.ToString().Trim();
|
record.Avisering = exObject.ReadCell(row, col).ToString().Trim();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
@ -6,14 +6,22 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</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>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\WinFormDi.BLI\WinFormDiApp.BLI.csproj" />
|
<ProjectReference Include="..\WinFormDi.BLI\WinFormDiApp.BLI.csproj" />
|
||||||
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
|
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
|
||||||
<ProjectReference Include="..\WinFormDiApp.DAL\WinFormDiApp.DAL.csproj" />
|
<ProjectReference Include="..\WinFormDiApp.DAL\WinFormDiApp.DAL.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="EPPlus" Version="6.2.4" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -40,6 +40,7 @@ namespace WinFormDiApp
|
|||||||
.AddTransient<IAccountRecordRepository,AccountRecordRepository>()
|
.AddTransient<IAccountRecordRepository,AccountRecordRepository>()
|
||||||
.AddTransient<IMemberRepository,MemberRepository>()
|
.AddTransient<IMemberRepository,MemberRepository>()
|
||||||
.AddTransient<IReadingIn, ReadingIn>()
|
.AddTransient<IReadingIn, ReadingIn>()
|
||||||
|
.AddTransient<IExcel, Excel>()
|
||||||
.AddTransient<MainWindow>()
|
.AddTransient<MainWindow>()
|
||||||
.AddTransient<frmReadPayments>()
|
.AddTransient<frmReadPayments>()
|
||||||
.AddTransient<frmPayments>();
|
.AddTransient<frmPayments>();
|
||||||
|
|||||||
Reference in New Issue
Block a user