Ejemplo n.º 1
0
        public ActionResult ActualizarDatosEmpresa(Dir_Empresas empresa)
        {
            using (Entities db = new Entities()) {
                if (ModelState.IsValid)
                {
                    try
                    {
                        db.Entry(empresa).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    catch (Exception e) {
                        return Json(e.Message);
                    }

                    return Json(1);
                }
            }
            return Json(0);
        }
Ejemplo n.º 2
0
        public ActionResult NuevaEmpresa(Dir_Empresas empresa)
        {
            using (Entities db = new Entities())
            {
                if (ModelState.IsValid)
                {
                    if (db.Dir_Empresas.Find(empresa.RutEmpresa) == null)
                    {
                        try
                        {
                            db.Dir_Empresas.Add(empresa);
                            db.SaveChanges();
                        }
                        catch (Exception e) {
                            return Json(e.Message);
                        }

                        return Json(1);
                    }
                    else
                        return Json(0);

                }
            }
            return Json(0);
        }
Ejemplo n.º 3
0
        public ActionResult ImportarEmpresas(HttpPostedFileBase ExcelFile)
        {
            if (ExcelFile == null || ExcelFile.ContentLength == 0)
            {
                ViewBag.Error("Por favor ingrese un archivo de excel");
                return View("Empresas");
            }
            else
            {
                if (ExcelFile.FileName.EndsWith("xls") || ExcelFile.FileName.EndsWith("xlsx"))
                {
                    string path = Server.MapPath("~/Uploads/" + ExcelFile.FileName);
                    if (System.IO.File.Exists(path))
                        System.IO.File.Delete(path);
                    ExcelFile.SaveAs(path);
                    // leer informacion desde archivo excel
                    Excel.Application application = new Excel.Application();
                    Excel.Workbook workbook = application.Workbooks.Open(path);
                    Excel.Worksheet worksheet = workbook.ActiveSheet;
                    Excel.Range range = worksheet.UsedRange;

                    List<Dir_Empresas> ListaDeEmpresas = new List<Dir_Empresas>();
                    int contador = 0;
                    using (Entities db = new Entities())
                    {
                        for (int row = 3; row < range.Rows.Count; row++)
                        {
                            int rut = Convert.ToInt32(((Excel.Range)range.Cells[row, 1]).Text);
                            string dv = ((Excel.Range)range.Cells[row, 2]).Text;
                            // validacion con digito verificador
                            if (db.Dir_Empresas.Find(rut) == null)
                            {
                                Dir_Empresas emp = new Dir_Empresas();
                                emp.RutEmpresa = Convert.ToInt32(((Excel.Range)range.Cells[row, 1]).Text);
                                emp.DvEmpresa = ((Excel.Range)range.Cells[row, 2]).Text;
                                emp.RazonSocial = ((Excel.Range)range.Cells[row, 5]).Text;
                                emp.RepresentanteLegal = ((Excel.Range)range.Cells[row, 30]).Text;
                                emp.CodComuna = Convert.ToInt32(((Excel.Range)range.Cells[row, 19]).Text);
                                emp.IdEstado = 1;
                                // usar procedimientos almacenado para insertar lista de empresas
                                //var a = db.Pa_Cls_EmpresasDir_Insertar(emp.RutEmpresa, emp.CodComuna, emp.DvEmpresa, emp.RazonSocial, emp.RepresentanteLegal, emp.IdEstado);
                                db.Dir_Empresas.Add(emp);

                            }
                            contador++;
                        }
                        db.SaveChanges();
                    }
                    return View("Empresas");
                }
                else
                {
                    ViewBag.Error("El tipo de archivo  es incorrecto");
                }
            }
            return View();
        }