Beispiel #1
0
        public IActionResult SubirExcel([FromForm] FormularioExcel a)
        {
            ViewData["Api"] = _endPoint;
            var us = HttpContext.Session.GetObjectFromJson <UsuarioWeb>("Login");

            if (!ComprobarLogin())
            {
                return(RedirectToAction(_defaultPage, _defaultController));
            }
            else
            {
                ViewData["Usuario"] = HttpContext.Session.GetObjectFromJson <UsuarioWeb>("Login");
            }
            var msj = new List <MensajeError>();
            var res = new List <ArticuloBasico>();

            if (a.files != null)
            {
                try
                {
                    using (var ms = new MemoryStream())
                    {
                        a.files.CopyTo(ms);
                        var    fileBytes = ms.ToArray();
                        string s         = Convert.ToBase64String(fileBytes);
                        // act on the Base64 data

                        using (ExcelPackage package = new ExcelPackage(ms))
                        {
                            ExcelWorksheet worksheet  = package.Workbook.Worksheets[0];
                            int            rowCount   = worksheet.Dimension.Rows;
                            int            ColCount   = worksheet.Dimension.Columns;
                            bool           bHeaderRow = true;
                            for (int row = 1; row <= rowCount; row++)
                            {
                                if (bHeaderRow)
                                {
                                    bHeaderRow = false;
                                }
                                else
                                {
                                    var ar = new ArticuloBasico();
                                    //bool eurocode = true;
                                    if (worksheet.Cells[row, 1].Value != null)
                                    {
                                        var r = worksheet.Cells["C1"].Start;
                                        ar.Descripcion = worksheet.Cells[row, 1].Value.ToString();
                                        if (worksheet.Cells[row, 2].Value != null)
                                        {
                                            ar.Cantidad = worksheet.Cells[row, 2].Value.ToString();
                                        }
                                        else
                                        {
                                            ar.Cantidad = "1";
                                        }
                                        res.Add(ar);
                                    }

                                    /*for (int col = 1; col <= ColCount; col++)
                                     * {
                                     *  if (eurocode)
                                     *  {
                                     *      ar.Descripcion = worksheet.Cells[row, col].Value.ToString();
                                     *      eurocode = false;
                                     *  }
                                     *  else
                                     *  {
                                     *      ar.Cantidad = worksheet.Cells[row, col].Value.ToString();
                                     *      //sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
                                     *  }
                                     * }*/
                                }
                            }
                            var i = 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    msj.Add(new MensajeError {
                        Descripcion = "Archivo no válido", Estado = 2
                    });
                }

                if (res.Count > 0)
                {
                    var ad = new AdaptadorAtc(_configuration);
                    msj = ad.CarritosUsuariosAnadirMasivamente(res, a.vaciar, us.IdUsuarioWeb);
                }
                else
                {
                    msj.Add(new MensajeError {
                        Descripcion = "Excel vacío", Estado = 2
                    });
                }
            }
            else
            {
                msj.Add(new MensajeError {
                    Descripcion = "Archivo no válido", Estado = 2
                });
            }



            ViewData["Mensajes"] = msj;
            return(View());
        }