public IActionResult Relatorios(string dataMin, string dataFim, string tipo)
        {
            try
            {
                if (string.IsNullOrEmpty(dataMin) || string.IsNullOrEmpty(dataFim) || string.IsNullOrEmpty(tipo))
                {
                    throw new Exception("Erro. Todos os campos devem estar preenchidos.");
                }

                switch (int.Parse(tipo))
                {
                case 1:     //PDF
                    var pdf = ContaReportPdf.GenerateReport
                                  (contaRepository.GetByDatas(DateTime.Parse(dataMin), DateTime.Parse(dataFim)));

                    //DOWNLOAD DO DOCUMENTO PDF..
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.Headers.Add("content-disposition", "attachment; filename=Contas.pdf");
                    Response.Body.WriteAsync(pdf, 0, pdf.Length);
                    Response.Body.Flush();
                    Response.StatusCode = StatusCodes.Status200OK;

                    break;

                case 2:     //EXCEL

                    var excel = ContaReportExcel.GenerateReport
                                    (contaRepository.GetByDatas(DateTime.Parse(dataMin), DateTime.Parse(dataFim)));

                    //DOWNLOAD DO DOCUMENTO EXCEL..
                    Response.Clear();
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.Headers.Add("content-disposition", "attachment: filename=contas.xlsx");
                    Response.Body.WriteAsync(excel, 0, excel.Length);
                    Response.Body.Flush();
                    Response.StatusCode = StatusCodes.Status200OK;

                    break;
                }
            }
            catch (Exception e)
            {
                TempData["MensagemErro"] = e.Message;
            }

            return(View());
        }
Exemplo n.º 2
0
        //método para gerar o relatorio em formato EXCEL
        public void GerarRelatorioExcel([FromServices] IContaRepository contaRepository)
        {
            try
            {
                //consultar as contas no banco de dados
                var contas = contaRepository.GetAll();

                //gerando o relatorio
                var arquivo = ContaReportExcel.GenerateReport(contas);

                //DOWNLOAD do arquivo
                Response.Clear();
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.Headers.Add("content-disposition", "attachment; filename=contas.xlsx");
                Response.Body.WriteAsync(arquivo, 0, arquivo.Length);
                Response.Body.Flush();
                Response.StatusCode = StatusCodes.Status200OK;
            }
            catch (Exception e)
            {
                TempData["MensagemErro"] = e.Message;
            }
        }