Esempio n. 1
0
        public ActionResult Create(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    //ExcelDataReader works on binary excel file
                    Stream stream = file.InputStream;
                    //We need to written the Interface.
                    IExcelDataReader reader = null;
                    if (file.FileName.EndsWith(".xls"))
                    {
                        //reads the excel file with .xls extension
                        reader = ExcelReaderFactory.CreateBinaryReader(stream);
                    }
                    else if (file.FileName.EndsWith(".xlsx"))
                    {
                        //reads excel file with .xlsx extension
                        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    }
                    else
                    {
                        //Shows error if uploaded file is not Excel file
                        ModelState.AddModelError("File", "This file format is not supported");
                        return(View());
                    }
                    //treats the first row of excel file as Coluymn Names
                    reader.IsFirstRowAsColumnNames = true;
                    //Adding reader data to DataSet()
                    DataSet result = reader.AsDataSet();
                    reader.Close();
                    //Sending result data to database

                    var finacialYear = _financialYearAppService.GetActiveFinancialYear();
                    var station      = _stationAppService.GetStation(_userAppService.GetLoggedInUser().StationId);// get logged user station

                    _candidateAppService.UploadCandidates(result.Tables[0], finacialYear, station);
                    //return View();
                }
            }
            else
            {
                ModelState.AddModelError("File", "Please upload your file");
            }

            return(RedirectToAction("Create"));
        }