public static DataTable GetDataTableFromSpreadsheet(Stream MyExcelStream, bool ReadOnly) { DataTable dt = new DataTable(); using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(MyExcelStream, ReadOnly)) { WorkbookPart workbookPart = sDoc.WorkbookPart; IEnumerable<Sheet> sheets = sDoc.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); string relationshipId = sheets.First().Id.Value; WorksheetPart worksheetPart = (WorksheetPart)sDoc.WorkbookPart.GetPartById(relationshipId); Worksheet workSheet = worksheetPart.Worksheet; SheetData sheetData = workSheet.GetFirstChild<SheetData>(); IEnumerable<Row> rows = sheetData.Descendants<Row>(); foreach (Cell cell in rows.ElementAt(0)) { dt.Columns.Add(GetCellValue(sDoc, cell)); } foreach (Row row in rows) //this will also include your header row... { DataRow tempRow = dt.NewRow(); for (int i = 0; i < row.Descendants<Cell>().Count(); i++) { if (tempRow[i] == DBNull.Value || String.IsNullOrWhiteSpace(tempRow[i].ToString())) { continue; } else { tempRow[i] = GetCellValue(sDoc, row.Descendants<Cell>().ElementAt(i)); } } dt.Rows.Add(tempRow); } } dt.Rows.RemoveAt(0); return dt; }
if (tempRow[i] == DBNull.Value || String.IsNullOrWhiteSpace(tempRow[i].ToString()))
if (DBNull.Value.Equals(tempRow[i]) || String.IsNullOrWhiteSpace(tempRow[i].ToString()))
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)