public SLExcelData ReadExcel(Stream inputStream) { var data = new SLExcelData( ); // Check if the file is excel if (inputStream.Length <= 0) { data.Status.Message = "You uploaded an empty file"; return(data); } /*if ( file. * != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) * { * data.Status.Message * = "Please upload a valid excel file of version 2007 and above"; * return data; * }*/ // Open the excel document WorkbookPart workbookPart; List <Row> rows; try { var document = SpreadsheetDocument.Open(inputStream, false); workbookPart = document.WorkbookPart; var sheets = workbookPart.Workbook.Descendants <Sheet>( ); var sheet = sheets.First( ); data.SheetName = sheet.Name; var workSheet = (( WorksheetPart )workbookPart .GetPartById(sheet.Id)).Worksheet; var columns = workSheet.Descendants <Columns>( ).FirstOrDefault( ); data.ColumnConfigurations = columns; var sheetData = workSheet.Elements <SheetData>( ).First( ); rows = sheetData.Elements <Row>( ).ToList( ); } catch (Exception e) { data.Status.Message = "Unable to open the file"; return(data); } // Read the header if (rows.Count > 0) { var row = rows[0]; var cellEnumerator = GetExcelCellEnumerator(row); while (cellEnumerator.MoveNext( )) { var cell = cellEnumerator.Current; var text = ReadExcelCell(cell, workbookPart).Trim( ); data.Headers.Add(text); } } // Read the sheet data if (rows.Count > 1) { for (var i = 1; i < rows.Count; i++) { var dataRow = new List <string>( ); data.DataRows.Add(dataRow); var row = rows[i]; var cellEnumerator = GetExcelCellEnumerator(row); while (cellEnumerator.MoveNext( )) { var cell = cellEnumerator.Current; var text = ReadExcelCell(cell, workbookPart).Trim( ); dataRow.Add(text); } } } return(data); }
public SLExcelData ReadExcel( Stream inputStream ) { var data = new SLExcelData( ); // Check if the file is excel if ( inputStream.Length <= 0 ) { data.Status.Message = "You uploaded an empty file"; return data; } /*if ( file. != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) { data.Status.Message = "Please upload a valid excel file of version 2007 and above"; return data; }*/ // Open the excel document WorkbookPart workbookPart; List<Row> rows; try { var document = SpreadsheetDocument.Open( inputStream, false ); workbookPart = document.WorkbookPart; var sheets = workbookPart.Workbook.Descendants<Sheet>( ); var sheet = sheets.First( ); data.SheetName = sheet.Name; var workSheet = ( ( WorksheetPart ) workbookPart .GetPartById( sheet.Id ) ).Worksheet; var columns = workSheet.Descendants<Columns>( ).FirstOrDefault( ); data.ColumnConfigurations = columns; var sheetData = workSheet.Elements<SheetData>( ).First( ); rows = sheetData.Elements<Row>( ).ToList( ); } catch ( Exception e ) { data.Status.Message = "Unable to open the file"; return data; } // Read the header if ( rows.Count > 0 ) { var row = rows[ 0 ]; var cellEnumerator = GetExcelCellEnumerator( row ); while ( cellEnumerator.MoveNext( ) ) { var cell = cellEnumerator.Current; var text = ReadExcelCell( cell, workbookPart ).Trim( ); data.Headers.Add( text ); } } // Read the sheet data if ( rows.Count > 1 ) { for ( var i = 1; i < rows.Count; i++ ) { var dataRow = new List<string>( ); data.DataRows.Add( dataRow ); var row = rows[ i ]; var cellEnumerator = GetExcelCellEnumerator( row ); while ( cellEnumerator.MoveNext( ) ) { var cell = cellEnumerator.Current; var text = ReadExcelCell( cell, workbookPart ).Trim( ); dataRow.Add( text ); } } } return data; }