Esempio n. 1
0
        public ActionResult CarregarArquivo(DadosImportacaoVM model)
        {
            if (ModelState.IsValid)
            {
                this.ValidarExtensaoArquivo(model.Arquivo);

                if (ModelState.IsValid)
                {
                    MemoryStream target = new MemoryStream();

                    model.Arquivo.InputStream.CopyTo(target);

                    using (ExcelPackage package = new ExcelPackage(target))
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                        this.ValidarCabecalhoArquivoImportado(worksheet);

                        if (ModelState.IsValid)
                        {
                            var linhasArquivoImportado = this.LerArquivo(model, worksheet);
                            var relatorioLeituraVM     = new RelatorioLeituraVM(linhasArquivoImportado);

                            TempData["RelatorioLeitura"] = relatorioLeituraVM;
                            TempData["Categoria"]        = (model.Categoria.HasValue) ? model.Categoria.ToString() : null;
                            TempData["NomeArquivo"]      = model.Arquivo.FileName;

                            return(RedirectToAction("ExibirRelatorioLeitura", "Importacao"));
                        }

                        package.Dispose();
                    }

                    target.Close();
                }
            }

            this.PersistirDadosEmMemoria();

            return(View(model));
        }
        public static List <LinhaPlanilhaModel> LerArquivo(this ImportacaoController controller, DadosImportacaoVM model, ExcelWorksheet worksheet)
        {
            List <LinhaPlanilhaModel> linhasArquivoImportado = new List <LinhaPlanilhaModel>();

            var colunas = System.Enum.GetValues(typeof(EnumColunasPlanilha)).Cast <int>().ToList();

            for (var linha = LINHA_INICIO; linha <= worksheet.Dimension.Rows; linha++)
            {
                var celulas = new List <CelulaPlanilhaModel>();

                colunas.ForEach(x =>
                {
                    var celula = worksheet.Cells[linha, x].Value;
                    celulas.Add(new CelulaPlanilhaModel
                    {
                        Linha           = linha,
                        Coluna          = x,
                        ValorOriginal   = celula,
                        Endereco        = ExcelAddress.GetAddress(linha, x),
                        ValorManipulado = celula != null ? celula.ToString() : null
                    });
                });

                controller.ValidarCelulas(celulas);

                var tipoErroLinhaPlanilha = controller.ValidarRegraCNPJouCPF(celulas);

                linhasArquivoImportado.Add(new LinhaPlanilhaModel()
                {
                    ExcelRow = worksheet.Row(linha),
                    Celulas  = celulas,
                    Erro     = tipoErroLinhaPlanilha
                });
            }

            return(linhasArquivoImportado);
        }