示例#1
0
        public string verificarExcel()
        {
            try
            {
                List <FormatoExcelStock> datosIngresados = new List <FormatoExcelStock>();
                if (Request != null)
                {
                    HttpPostedFileBase file = Request.Files["file-0"];
                    if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                    {
                        string fileName        = file.FileName;
                        string fileContentType = file.ContentType;
                        byte[] fileBytes       = new byte[file.ContentLength];
                        var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                        using (var package = new ExcelPackage(file.InputStream))
                        {
                            var currentSheet = package.Workbook.Worksheets;
                            var workSheet    = currentSheet.First();
                            var noOfCol      = workSheet.Dimension.End.Column;
                            var noOfRow      = workSheet.Dimension.End.Row;

                            for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                            {
                                FormatoExcelStock datos = new FormatoExcelStock();
                                datos.codigo        = workSheet.Cells[rowIterator, 1].Value.ToString();
                                datos.descripcion   = workSheet.Cells[rowIterator, 2].Value.ToString();
                                datos.saldo         = double.Parse(workSheet.Cells[rowIterator, 3].Value.ToString());
                                datos.costoUnitario = double.Parse(workSheet.Cells[rowIterator, 4].Value.ToString());
                                datos.bodega        = workSheet.Cells[rowIterator, 5].Value.ToString();
                                datosIngresados.Add(datos);
                            }
                        }
                    }
                }
                return("true");
            }
            catch (Exception e)
            {
                return(e.Message);
            }
        }
示例#2
0
        public ActionResult ActualizarExcel(FormCollection formCollection)
        {
            List <FormatoExcelStock> datosIngresados = new List <FormatoExcelStock>();

            if (Request != null)
            {
                HttpPostedFileBase file = Request.Files["UploadedFile"];
                if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
                {
                    string fileName        = file.FileName;
                    string fileContentType = file.ContentType;
                    byte[] fileBytes       = new byte[file.ContentLength];
                    var    data            = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
                    using (var package = new ExcelPackage(file.InputStream))
                    {
                        var currentSheet = package.Workbook.Worksheets;
                        var workSheet    = currentSheet.First();
                        var noOfCol      = workSheet.Dimension.End.Column;
                        var noOfRow      = workSheet.Dimension.End.Row;

                        for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                        {
                            FormatoExcelStock datos = new FormatoExcelStock();
                            datos.codigo        = workSheet.Cells[rowIterator, 1].Value.ToString();
                            datos.descripcion   = workSheet.Cells[rowIterator, 2].Value.ToString();
                            datos.saldo         = double.Parse(workSheet.Cells[rowIterator, 3].Value.ToString());
                            datos.costoUnitario = double.Parse(workSheet.Cells[rowIterator, 4].Value.ToString());
                            datos.bodega        = workSheet.Cells[rowIterator, 5].Value.ToString();
                            datosIngresados.Add(datos);
                        }
                    }
                    ViewBag.Users = datosIngresados;
                }
            }
            FormatoExcelStock.agregar(datosIngresados);

            return(RedirectToAction("Index"));
        }