コード例 #1
0
        private static FileResult GenerarReportePdfFileResult(
            string nombreReporte,
            string titulo,
            IEnumerable datos,
            Dictionary <string, string> parametrosAdicionales = null)
        {
            var reportViewer = new ReportViewer();

            reportViewer.LocalReport.ReportPath           = ObtenerPathReporte(nombreReporte);
            reportViewer.LocalReport.EnableExternalImages = true;
            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("Datos", datos));

            var parametros = new List <ReportParameter>
            {
                new ReportParameter("Titulo", titulo),
                new ReportParameter("ColorPrincipal", Configuracion.Configuracion.ColorPrincipal),
                new ReportParameter("LogoURL", Configuracion.Configuracion.LogoURL),
                new ReportParameter("MutualNombre", Configuracion.Configuracion.MutualNombre),
                new ReportParameter("MutualDireccion", Configuracion.Configuracion.MutualDireccion.Replace(@"\n", Environment.NewLine))
            };

            if (parametrosAdicionales != null)
            {
                foreach (var parametro in parametrosAdicionales)
                {
                    parametros.Add(new ReportParameter(parametro.Key, parametro.Value));
                }
            }

            reportViewer.LocalReport.SetParameters(parametros);

            var resultado = new FileContentResult(reportViewer.LocalReport.Render("PDF"), "application/pdf");

            resultado.FileDownloadName = String.Format("{0}-{1:ddMMyyyyHHmm}.pdf", Formato.FormatoSlug(titulo), DateTime.Now);
            return(resultado);
        }
コード例 #2
0
        public static FileResult GenerarReporteExcelFileResult <TModelo>(
            IEnumerable <TModelo> items,
            string titulo, string[] encabezados, Func <TModelo, object[]> mapeoItemValores,
            Dictionary <string, object> valoresAdicionales = null)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            if (String.IsNullOrWhiteSpace(titulo))
            {
                throw new ArgumentNullException("nombre");
            }
            if (encabezados == null || !encabezados.Any())
            {
                throw new ArgumentNullException("titulos");
            }
            if (mapeoItemValores == null)
            {
                throw new ArgumentNullException("mapeoItemValores");
            }

            using (var pkg = new ExcelPackage())
            {
                var hoja = pkg.Workbook.Worksheets.Add(titulo);

                int tituloCol = 1;
                foreach (var encabezado in encabezados)
                {
                    hoja.Cells[1, tituloCol].Value = encabezado;
                    tituloCol++;
                }

                var titulosEstilos = hoja.Cells[1, 1, 1, encabezados.Length].Style;
                titulosEstilos.Font.Bold        = true;
                titulosEstilos.Fill.PatternType = ExcelFillStyle.Solid;
                titulosEstilos.Fill.BackgroundColor.SetColor(Color.LightGray);

                int fila    = 2,
                    columna = 1;

                object[] valores = null;

                foreach (var item in items)
                {
                    valores = mapeoItemValores(item);
                    columna = 1;

                    foreach (var valor in valores)
                    {
                        hoja.Cells[fila, columna].Value = valor;
                        columna++;
                    }

                    fila++;
                }

                if (valoresAdicionales != null)
                {
                    foreach (var valor in valoresAdicionales)
                    {
                        hoja.Cells[fila, 1].Value           = valor.Key + ":";
                        hoja.Cells[fila, 1].Style.Font.Bold = true;

                        hoja.Cells[fila, 2].Value = valor.Value;

                        fila++;
                    }
                }

                hoja.Cells[1, 1, fila, encabezados.Length].AutoFitColumns();

                var resultado = new FileContentResult(pkg.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                resultado.FileDownloadName = String.Format("{0}-{1:ddMMyyyyHHmm}.xlsx", Formato.FormatoSlug(titulo), DateTime.Now);
                return(resultado);
            }
        }