public CellReadResponse<decimal> ReadCellAsDecimal(int sheetIndex, int rowIndex, int columnIndex) { var result = new CellReadResponse<decimal> { Status = CellReadStatus.Success }; if (document.WorkbookPart.WorksheetParts.Count() <= sheetIndex) { result.Status = CellReadStatus.EmptyCell; } else { var sheetData = document.WorkbookPart.WorksheetParts.ElementAt(sheetIndex).Worksheet.Elements<SheetData>().Single(); var cell = sheetData.Descendants<Cell>().SingleOrDefault(c => c.CellReference == cellReferenceBuilder.GetCellReference(rowIndex, columnIndex)); if (cell == null || cell.CellValue == null || string.IsNullOrEmpty(cell.CellValue.Text)) { result.Status = CellReadStatus.EmptyCell; } else if (cell.DataType != null && cell.DataType != CellValues.Number) { result.Status = CellReadStatus.WrongDataType; } else { result.Value = (decimal)Convert.ToDouble(cell.CellValue.Text); } } return result; }
public CellReadResponse<string> ReadCellAsString(int sheetIndex, int rowIndex, int columnIndex) { var result = new CellReadResponse<string> { Status = CellReadStatus.Success }; if (document.WorkbookPart.WorksheetParts.Count() <= sheetIndex) { result.Status = CellReadStatus.EmptyCell; } else { var sheetData = document.WorkbookPart.WorksheetParts.ElementAt(sheetIndex).Worksheet.Elements<SheetData>().Single(); var cell = sheetData.Descendants<Cell>().SingleOrDefault(c => c.CellReference == cellReferenceBuilder.GetCellReference(rowIndex, columnIndex)); if (cell == null || cell.CellValue == null || string.IsNullOrEmpty(cell.CellValue.Text)) { result.Status = CellReadStatus.EmptyCell; } else if (cell.DataType == null || (cell.DataType != CellValues.String && cell.DataType != CellValues.SharedString)) { result.Status = CellReadStatus.WrongDataType; } else { if (cell.DataType == CellValues.SharedString) { var sharedStringIndex = Convert.ToInt32(cell.CellValue.Text); var sharedString = document.WorkbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(sharedStringIndex); result.Value = sharedString.Text.Text; } else { result.Value = cell.CellValue.Text; } } } return result; }