public void ReadFromExcel(MemoryStream excelFileStream, MoscowWeatherContext dbConnection)
        {
            excelFileStream.Position = 0;
            var workBook = new XSSFWorkbook(excelFileStream);

            var excelRowDtos = new List <ExcelRowDto>();

            for (var sheetIndex = 0; sheetIndex < workBook.NumberOfSheets; sheetIndex++)
            {
                var sheet = workBook.GetSheetAt(sheetIndex);

                ProcessSheetHeaders(sheet);
                excelRowDtos.AddRange(GetDataFromRowColumns(sheet));
            }

            ProcessRows(excelRowDtos, dbConnection);
        }
        private void ProcessRows(List <ExcelRowDto> excelRowDtos, MoscowWeatherContext dbConnection)
        {
            foreach (var dto in excelRowDtos)
            {
                var weatherModel = new Weather();

                if (!string.IsNullOrEmpty(dto.RowColumns[0]) && !string.IsNullOrWhiteSpace(dto.RowColumns[0]))
                {
                    weatherModel.Date = DateTime.ParseExact(dto.RowColumns[0], "dd.MM.yyyy", CultureInfo.InvariantCulture);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[1]) && !string.IsNullOrWhiteSpace(dto.RowColumns[1]))
                {
                    weatherModel.Time = TimeSpan.Parse(dto.RowColumns[1]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[2]) && !string.IsNullOrWhiteSpace(dto.RowColumns[2]))
                {
                    weatherModel.Temperature = decimal.Parse(dto.RowColumns[2]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[3]) && !string.IsNullOrWhiteSpace(dto.RowColumns[3]))
                {
                    weatherModel.Humidity = decimal.Parse(dto.RowColumns[3]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[4]) && !string.IsNullOrWhiteSpace(dto.RowColumns[4]))
                {
                    weatherModel.Td = decimal.Parse(dto.RowColumns[4]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[5]) && !string.IsNullOrWhiteSpace(dto.RowColumns[5]))
                {
                    weatherModel.AtmosphericPressure = decimal.Parse(dto.RowColumns[5]);
                }

                weatherModel.WindDirection = (dto.RowColumns[6]);

                if (!string.IsNullOrEmpty(dto.RowColumns[7]) && !string.IsNullOrWhiteSpace(dto.RowColumns[7]))
                {
                    weatherModel.WindSpeed = decimal.Parse(dto.RowColumns[7]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[8]) && !string.IsNullOrWhiteSpace(dto.RowColumns[8]))
                {
                    weatherModel.Cloudiness = decimal.Parse(dto.RowColumns[8]);
                }

                if (!string.IsNullOrEmpty(dto.RowColumns[9]) && !string.IsNullOrWhiteSpace(dto.RowColumns[9]))
                {
                    weatherModel.H = decimal.Parse(dto.RowColumns[9]);
                }

                weatherModel.Vv = (dto.RowColumns[10]);

                weatherModel.WeatherCondition = dto.RowColumns[11];

                dbConnection.Add(weatherModel);
            }

            dbConnection.SaveChanges();
        }
Example #3
0
 public IndexModel(MoscowWeather.Models.MoscowWeatherContext context)
 {
     _context = context;
 }