From a52f1d54c738b9feb6e318d9a03d7dd7e9c003aa Mon Sep 17 00:00:00 2001 From: Dmitriy Benyuk Date: Mon, 8 Apr 2024 13:35:52 +0300 Subject: [PATCH 1/2] Read headers correctly (foreach skips empty columns) --- ...micweb.DataIntegration.Providers.ExcelProvider.csproj | 2 +- src/ExcelReader.cs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj index bb7d3cd..9f89728 100644 --- a/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.ExcelProvider.csproj @@ -1,6 +1,6 @@  - 10.0.14 + 10.0.15 1.0.0 Excel Provider Excel Provider diff --git a/src/ExcelReader.cs b/src/ExcelReader.cs index 5c58cdc..e300400 100644 --- a/src/ExcelReader.cs +++ b/src/ExcelReader.cs @@ -43,9 +43,10 @@ private void LoadExcelFile() var emptyRows = new List(); 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 firstRow = worksheet.Cells[1, 1, 1, worksheet.Dimension.End.Column]; + for (var colNum = 1; colNum <= worksheet.Dimension.End.Column; colNum++) { + var firstRowCell = firstRow[1, colNum]; DataColumn column; var header = hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column); if (!dataTable.Columns.Contains(header) && !string.IsNullOrWhiteSpace(header)) @@ -54,15 +55,13 @@ private void LoadExcelFile() } 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; From 02b46dce14e3aecefa2aae07b446f35f43685d6f Mon Sep 17 00:00:00 2001 From: Dmitriy Benyuk Date: Mon, 8 Apr 2024 14:16:00 +0300 Subject: [PATCH 2/2] Start reading data from the row where there is any data --- src/ExcelReader.cs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ExcelReader.cs b/src/ExcelReader.cs index e300400..eb9d078 100644 --- a/src/ExcelReader.cs +++ b/src/ExcelReader.cs @@ -40,15 +40,18 @@ 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(); - var dataTable = new DataTable(worksheet.Name); - var hasHeader = true; - var firstRow = 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[1, 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); @@ -63,9 +66,8 @@ private void LoadExcelFile() column.Caption = firstRowCell.Comment.Text; } } - - 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]; @@ -92,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();