Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

C# OPEN XML: Empty Rows are getting skipped while getting data from EXCEL to DATATABLE

$
0
0

Task

Import data from excel to DataTable

Problem

Some of Rows that does not contain any data are getting skipped and the very next Row that has data in the row is used as the value of the empty Row

E.g

In Excel Totally in have 37 Rows when i use openxml to convert excel to Datatable it skipped empty rows and read 29 rows only

WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
Worksheet workSheet = worksheetPart.Worksheet;
SheetData sheetData = workSheet.GetFirstChild<SheetData>();
IEnumerable<Row> rows = sheetData.Descendants<Row>();
foreach (Row row in rows) //this will also include your header row...
{
    DataRow tempRow = dt.NewRow();
    int ko = row.Descendants<Cell>().Count();
    for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
    {
        tempRow[i] = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
    }
    dt.Rows.Add(tempRow);
}

Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>