Skip to content

Commit

Permalink
Merge pull request #24 from dynamicweb/dbe/18883-Read-Headers-Correctly
Browse files Browse the repository at this point in the history
Read headers correctly (foreach skips empty columns)
  • Loading branch information
frederik5480 authored Apr 11, 2024
2 parents 8aa64e8 + 02b46dc commit 91f550b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.14</Version>
<Version>10.0.15</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<Title>Excel Provider</Title>
<Description>Excel Provider</Description>
Expand Down
38 changes: 27 additions & 11 deletions src/ExcelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,34 @@ private void LoadExcelFile()
var ds = new DataSet();
foreach (var worksheet in package.Workbook.Worksheets)
{
var firstRowNumberWithData = GetFirstRowNumberWithData(worksheet);
if (firstRowNumberWithData <= 0)
continue;

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])
var dataTable = new DataTable(worksheet.Name);
var firstRow = worksheet.Cells[firstRowNumberWithData, 1, firstRowNumberWithData, worksheet.Dimension.End.Column];
for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
{
var firstRowCell = firstRow[firstRowNumberWithData, colNum];
DataColumn column;
var header = hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column);
var header = firstRowCell.Text;
if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header))
{
column = dataTable.Columns.Add(header);
}
else
{
column = dataTable.Columns.Add(header + i);
column = dataTable.Columns.Add(header + colNum);
}

if (!string.IsNullOrEmpty(firstRowCell.Comment?.Text))
{
column.Caption = firstRowCell.Comment.Text;
}

i++;
}

var startRow = hasHeader ? 2 : 1;
for (var rowNum = startRow; rowNum <= worksheet.Dimension.End.Row; rowNum++)

for (var rowNum = firstRowNumberWithData + 1; rowNum <= worksheet.Dimension.End.Row; rowNum++)
{
var hasValue = false;
var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
Expand All @@ -93,6 +94,21 @@ private void LoadExcelFile()
ExcelSet = ds;
}

private static int GetFirstRowNumberWithData(ExcelWorksheet worksheet)
{
for (var rowNum = 1; rowNum <= worksheet.Dimension?.End?.Row; rowNum++)
{
var wsRow = worksheet.Cells[rowNum, 1, rowNum, worksheet.Dimension.End.Column];
for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++)
{
string cellText = wsRow[rowNum, colNum].Text;
if (!string.IsNullOrWhiteSpace(cellText))
return rowNum;
}
}
return -1;
}

public void Dispose()
{
ExcelSet.Clear();
Expand Down

0 comments on commit 91f550b

Please sign in to comment.