Exemplo n.º 1
0
        public ResponseData UploadStudents(IFormFile formFile)
        {
            try
            {
                if (formFile != null && formFile.Length > 0)
                {
                    Console.WriteLine("file name " + formFile.FileName);

                    XSSFWorkbook excelUpload;
                    using (var excelStream = formFile.OpenReadStream())
                        excelUpload = new XSSFWorkbook(excelStream);

                    var programme = _programmeRepository.ReadDefaultProgramme();

                    if (programme.Id == 0)
                    {
                        return(ResponseData.SendFailMsg("No programme exist yet. Kindly create at least one"));
                    }

                    var sheet = excelUpload.GetSheet("Students");

                    for (var row = 1; row <= sheet.LastRowNum; row++) // iterating through rows
                    {
                        if (!CreateStudent(new Student
                        {
                            FirstName = sheet.GetRow(row).GetCell(0).StringCellValue,
                            LastName = sheet.GetRow(row).GetCell(1).StringCellValue,
                            Email = sheet.GetRow(row).GetCell(2).StringCellValue,
                            MatricNumber = sheet.GetRow(row).GetCell(3).StringCellValue,
                            ProgrammeId = programme.Id
                        }))
                        {
                            return(ResponseData.SendFailMsg("Attempted to upload an Invalid student. Kindly retry with valid students"));
                        }
                    }

                    return(ResponseData.SendSuccessMsg());
                }

                return(ResponseData.SendFailMsg("Invalid upload file supplied"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(ResponseData.SendFailMsg("Error in uploading students. Kindly retry with valid students"));
            }
        }