Example #1
0
        public async Task <ActionResult <List <SheetAdmissionValidationErrors> > > PostFile(File file)
        {
            _context.File.Add(file);

            //Lê o arquivo EXCEL
            var datatable2 = _excelServices.ReadSheet(file.Data, 0);

            ActionResult <List <SheetAdmissionValidationErrors> > _lsheetAdmissionValidationErrorRetorno = new List <SheetAdmissionValidationErrors>();
            List <SheetAdmissionValidationErrors> _lsheetAdmissionValidationError = new List <SheetAdmissionValidationErrors>();
            List <SheetAdmission> ValidItems = new List <SheetAdmission>();
            //Valida o Arquivo de Adimissão
            var isValid = _sheetAdmissionValidation.Validate(datatable2, ref _lsheetAdmissionValidationError, ref ValidItems);

            _lsheetAdmissionValidationErrorRetorno = _lsheetAdmissionValidationError;

            if (!isValid)
            {
                //Retorna os itens do arquivo para correção
                return(Accepted(_lsheetAdmissionValidationError));
            }
            else
            {
                //Grava Arquivo
                var _file = await _fileService.AddAsync(file);

                _file.DateRegister = DateTime.Today;
                foreach (SheetAdmission lSheetAdmission in ValidItems)
                {
                    //Grava os itens do Arquivo
                    lSheetAdmission.FileId = _file.Id;
                    await _sheetAdmissionService.AddAsync(lSheetAdmission);
                }
            }

            return(CreatedAtAction("GetFile", new { id = file.Id }, file));
        }
Example #2
0
        // GET: Process/id
        public async Task <IActionResult> Process(int id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var file = await _fileService.GetByIdAsync(id);

            if (file == null)
            {
                return(NotFound());
            }

            var datatable2 = _excelServices.ReadSheet(file.Data, 0);

            List <SheetAdmissionValidationErrors> _lsheetAdmissionValidationError = new List <SheetAdmissionValidationErrors>();
            List <SheetAdmission> ValidItems = new List <SheetAdmission>();

            var isValid = _sheetAdmissionValidation.Validate(datatable2, ref _lsheetAdmissionValidationError, ref ValidItems);

            //Create a new workbook
            Workbook workbook = new Workbook();

            //Load a file and imports its data
            System.IO.Stream stream = new System.IO.MemoryStream(file.Data);
            workbook.LoadFromStream(stream);
            //Initialize worksheet
            Worksheet sheet = workbook.Worksheets[0];
            // get the data source that the grid is displaying data for
            DataTable datatable = sheet.ExportDataTable();

            DataRow[] oDataRow = datatable.Select();

            foreach (DataRow dr in oDataRow)
            {
                SheetAdmission lSheetAdmission = new SheetAdmission();
                lSheetAdmission.FileId = id;

                try
                {
                    lSheetAdmission.DeliveryDate = DateTime.Parse(dr[0].ToString());
                }
                catch (Exception e)
                {
                    //  Block of code to handle errors
                }

                try
                {
                    lSheetAdmission.Amount = Int32.Parse(dr[2].ToString());
                }
                catch (Exception e)
                {
                    //  Block of code to handle errors
                }

                lSheetAdmission.ProductName = dr[1].ToString();

                try
                {
                    lSheetAdmission.UnitaryValue = Decimal.Parse(dr[3].ToString());
                }
                catch (Exception e)
                {
                    //  Block of code to handle errors
                }

                string erros = "";
                _validationErros.FormatErros(lSheetAdmission, ref erros);

                //Adiciona Itens
                await _sheetAdmissionService.AddAsync(lSheetAdmission);
            }

            //List<SheetAdmission> lsheetAdmission = new List<SheetAdmission>();
            //var lsheetAdmission = _sheetAdmissionService.GetByIdIncludingTasksAsync(23);

            //List<ReportFileImports> reportFileImports = new List<ReportFileImports>();
            var reportFileImports = _importsRepository.GetAllImports();

            var reportFileImportsById = await _importsRepository.GetImportById(23);


            var FileStreamResult = FileDownload(file.Data, file.Name);

            return(FileStreamResult);
        }