Example #1
0
        public ActionResult Importarempleados(HttpPostedFileBase uploadfile)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int cont    = 0;
                    int IdCargo = 0;
                    List <rol_empleado_Info> listaemp      = new List <rol_empleado_Info>();
                    List <rol_cargo_Info>    listaCargo    = new List <rol_cargo_Info>();
                    rol_cargo_Data           caro_data     = new rol_cargo_Data();
                    rol_empleado_Data        empleado_data = new rol_empleado_Data();
                    if (uploadfile != null && uploadfile.ContentLength > 0)
                    {
                        Stream           stream = uploadfile.InputStream;
                        IExcelDataReader reader = null;
                        if (uploadfile.FileName.EndsWith(".xls"))
                        {
                            reader = ExcelReaderFactory.CreateBinaryReader(stream);
                        }
                        else if (uploadfile.FileName.EndsWith(".xlsx"))
                        {
                            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        }
                        while (reader.Read())
                        {
                            if (!reader.IsDBNull(0))
                            {
                                if (cont != 0)
                                {
                                    // verificando cargo
                                    if (cargo_data.si_existe(reader.GetString(7)) == false)
                                    {
                                        rol_cargo_Info infoc = new rol_cargo_Info();
                                        infoc.rc_descripcion = reader.GetString(7);
                                        infoc.estado         = true;
                                        caro_data.grabarDB(infoc, ref IdCargo);
                                    }
                                    else
                                    {
                                        IdCargo = cargo_data.GetInfo(reader.GetString(7)).IdCargo;
                                    }


                                    try
                                    {
                                        rol_empleado_Info info = new rol_empleado_Info();
                                        info.re_codigo    = reader.GetString(0);
                                        info.re_apellidos = reader.GetString(1);
                                        info.re_nombres   = reader.GetString(2);
                                        info.re_cedula    = reader.GetString(3);
                                        info.re_correo    = reader.GetString(4);
                                        info.re_telefonos = reader.GetString(5);
                                        info.re_direccion = reader.GetString(6);
                                        info.IdCargo      = IdCargo;
                                        info.estado       = reader.GetString(8) == "A" ? true : false;
                                        listaemp.Add(info);
                                        if (emp_data.si_existe(info.re_cedula))
                                        {
                                            emp_data.modificarDB_x_cedula(info);
                                        }
                                        else
                                        {
                                            empleado_data.guardarDB(info);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                                cont++;
                            }
                        }

                        return(RedirectToAction("Index", "Empleado"));
                    }
                }
                else
                {
                    ModelState.AddModelError("File", "Please upload your file");
                }
                return(View());
            }
            catch (Exception ex)
            {
                return(View());
            }
        }