Skip to content

Commit

Permalink
Merge pull request #20 from dynamicweb/frn/unable-to-load-tables
Browse files Browse the repository at this point in the history
Adding fallbacks to commercial license and properly skipping empty ce…
  • Loading branch information
frederik5480 authored Mar 25, 2024
2 parents 0833acf + fbc4268 commit b3e9f80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.10</Version>
<Version>10.0.11</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Title>Excel Provider</Title>
<Description>Excel Provider</Description>
Expand Down
1 change: 1 addition & 0 deletions src/ExcelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public override string ValidateDestinationSettings()

public override string ValidateSourceSettings()
{
ExcelPackage.LicenseContext = LicenseContext.Commercial;
if (SourceFile.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase) ||
SourceFile.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) ||
SourceFile.EndsWith(".xlsm", StringComparison.OrdinalIgnoreCase))
Expand Down
13 changes: 11 additions & 2 deletions src/ExcelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ExcelReader(string filename)

private void LoadExcelFile()
{
ExcelPackage.LicenseContext = LicenseContext.Commercial;
var fileInfo = new FileInfo(Filename);
using var package = new ExcelPackage(fileInfo);
var ds = new DataSet();
Expand All @@ -42,9 +43,15 @@ private void LoadExcelFile()
var emptyRows = new List<DataRow>();
var dataTable = new DataTable(worksheet.Name);
var hasHeader = true;
int i = 0;
foreach (var firstRowCell in worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column])
{
dataTable.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
var header = hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column);
if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header))
dataTable.Columns.Add(header);
else
dataTable.Columns.Add(header + i);
i++;
}

var startRow = hasHeader ? 2 : 1;
Expand All @@ -53,12 +60,14 @@ private void LoadExcelFile()
var hasValue = false;
var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
var row = dataTable.Rows.Add();
var c = 0;
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
row[c] = cell.Text;

if (!string.IsNullOrWhiteSpace(cell.Text))
hasValue = true;
c++;
}

if (!hasValue)
Expand Down

0 comments on commit b3e9f80

Please sign in to comment.