From 02b46dce14e3aecefa2aae07b446f35f43685d6f Mon Sep 17 00:00:00 2001 From: Dmitriy Benyuk Date: Mon, 8 Apr 2024 14:16:00 +0300 Subject: [PATCH] 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();