public ExcelData Read(Stream excelStream)
        {
            ExcelData excelData = new ExcelData();

            if (excelStream != null)
            {
                using (var excelDoc = SpreadsheetDocument.Open(excelStream, false))
                {
                    var rows = excelDoc.WorkbookPart.WorksheetParts.First()
                               .Worksheet.GetFirstChild <SheetData>().Descendants <Row>();

                    var cells = excelDoc.WorkbookPart.WorksheetParts.First()
                                .Worksheet.GetFirstChild <SheetData>().Descendants <Cell>();

                    int rowsCount  = rows.Count();
                    int cellsCount = cells.Count();

                    if (rowsCount > 0 && cellsCount > 0)
                    {
                        int rowSize = 'A' + (cellsCount / rowsCount);

                        int  fromRowID    = 1;
                        char fromColumnID = 'A';
                        for (int rowID = fromRowID; rowID <= rowsCount; rowID++)
                        {
                            ExcelRowData rowData = new ExcelRowData();
                            for (char columnID = fromColumnID; columnID < rowSize; columnID++)
                            {
                                string        cellAddress = columnID + rowID.ToString();
                                ExcelCellData cellData    = GetExcelCellData(excelDoc, cellAddress);
                                if (cellData != null)
                                {
                                    rowData.DataRow.Add(cellData);
                                }
                            }
                            if (rowID == fromRowID)
                            {
                                excelData.HeadingRow = rowData;
                            }
                            else
                            {
                                excelData.DataRows.Add(rowData);
                            }
                        }
                    }
                    else
                    {
                        throw new UtilsException("Couldn't read the Excel file.");
                    }
                }
            }
            else
            {
                throw new UtilsException("No data was provided for reading.");
            }
            return(excelData);
        }
Exemple #2
0
 public ExcelData()
 {
     this._headingRow = new ExcelRowData();
     this._dataRows   = new List <ExcelRowData>();
     this._allRows    = new List <ExcelRowData>();
 }