Esempio n. 1
0
        private static XSSFWorkbook GenerarExcelNPOI(PlantillaExcel Plantilla)
        {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet    sheet    = (XSSFSheet)workbook.CreateSheet("Resultado");

            bool CrearCabeceras = true;

            Plantilla.FilasCreadasIndice = 1;
            var DiccionarioColumnas = (Dictionary <string, string>)(Plantilla.CabeceraColumna);

            if (Plantilla.Datos != null && Plantilla.Datos.GetType().IsGenericType)
            {
                foreach (var FilaDato in (IList)Plantilla.Datos)
                {
                    PropertyInfo[] ColumnasDatos = FilaDato.GetType().GetProperties();

                    if (CrearCabeceras)
                    {
                        CrearTituloExcel(workbook, sheet, ColumnasDatos.Length, Plantilla);
                        CrearSeccionFiltro(workbook, sheet, ColumnasDatos, Plantilla);
                        Plantilla.FilasCreadasIndice++;
                        CrearSeccionCabeceraDatos(workbook, sheet, Plantilla, ColumnasDatos);
                        CrearCabeceras = false;
                    }

                    InsertarFilaDeDatos(workbook, sheet, Plantilla, ColumnasDatos, FilaDato);
                }
            }

            return(workbook);
        }
Esempio n. 2
0
        private static ICellStyle GenerarCellStyleTitulo(XSSFWorkbook workbook, PlantillaExcel Plantilla)
        {
            var style1 = workbook.CreateCellStyle();
            var font1  = workbook.CreateFont();

            if (Plantilla.EstiloTitulo.ColorLetra > 0)
            {
                font1.Color = Plantilla.EstiloTitulo.ColorLetra;
            }

            if (Plantilla.EstiloTitulo.ColorFondo > 0)
            {
                style1.FillForegroundColor = Plantilla.EstiloTitulo.ColorFondo;
                style1.FillPattern         = FillPattern.SolidForeground;
            }

            if (Plantilla.EstiloTitulo.Tamanio > 0)
            {
                font1.FontHeight = Plantilla.EstiloTitulo.Tamanio;
            }

            if (Plantilla.EstiloTitulo.EsNegrita)
            {
                font1.IsBold = Plantilla.EstiloTitulo.EsNegrita;
            }
            style1.SetFont(font1);

            return(style1);
        }
Esempio n. 3
0
 private static void GeneraArchivoEnBytes(ArchivoReporte Archivo, PlantillaExcel Plantilla)
 {
     using (var ArchivoEnMemoria = new MemoryStream())
     {
         var ArchivoExcel = GenerarExcelNPOI(Plantilla);
         ArchivoExcel.Write(ArchivoEnMemoria);
         Archivo.ArchivoByte = ArchivoEnMemoria.ToArray();
     }
 }
Esempio n. 4
0
        public static ArchivoReporte ExportarArchivo(PlantillaExcel Plantilla)
        {
            var resultado = new ArchivoReporte();

            GenerarNombreArchivoYContentType(resultado, Plantilla);
            GeneraArchivoEnBytes(resultado, Plantilla);

            return(resultado);
        }
Esempio n. 5
0
        private static void CrearSeccionCabeceraDatos(XSSFWorkbook workbook, XSSFSheet HojaActual, PlantillaExcel Plantilla, PropertyInfo[] ColumnasDatos)
        {
            XSSFRow rowHeaderGrid   = (XSSFRow)HojaActual.CreateRow(Plantilla.FilasCreadasIndice);
            int     ContadorColumna = 0;

            foreach (var itemCols in ColumnasDatos)
            {
                XSSFCell colGridExcel = (XSSFCell)rowHeaderGrid.CreateCell(ContadorColumna);
                if (Plantilla.EstiloCeldaCabecera != null)
                {
                    colGridExcel.CellStyle = GenerarCellStyleCeldaCabecera(workbook, Plantilla);
                }

                string colName  = Plantilla.CabeceraColumna.Any(x => x.Key == itemCols.Name) ? Plantilla.CabeceraColumna.FirstOrDefault(x => x.Key == itemCols.Name).Value : itemCols.Name;
                int    colWidth = Plantilla.TamanioColumna.Any(x => x.Key == itemCols.Name) ? Plantilla.TamanioColumna.FirstOrDefault(x => x.Key == itemCols.Name).Value : 80;

                colGridExcel.SetCellValue(new XSSFRichTextString(colName));
                colGridExcel.Sheet.DefaultColumnWidth = colWidth;
                ContadorColumna++;
            }

            Plantilla.FilasCreadasIndice++;
        }
Esempio n. 6
0
        private static void CrearSeccionFiltro(XSSFWorkbook workbook, XSSFSheet HojaActual, PropertyInfo[] ColumnasDatos, PlantillaExcel Plantilla)
        {
            XSSFRow FilaFiltros = null;

            if (Plantilla.Filtros != null)
            {
                foreach (var item in Plantilla.Filtros)
                {
                    FilaFiltros = (XSSFRow)HojaActual.CreateRow(Plantilla.FilasCreadasIndice);

                    var NuevaCelda = FilaFiltros.CreateCell(0);
                    if (Plantilla.EstiloFiltro != null)
                    {
                        NuevaCelda.CellStyle = GenerarCellStyleFiltro(workbook, Plantilla);
                    }

                    NuevaCelda.SetCellValue(item);
                    Plantilla.FilasCreadasIndice++;
                }
            }
        }
Esempio n. 7
0
        private static void CrearTituloExcel(XSSFWorkbook workbook, XSSFSheet HojaActual, int NumeroColumnas, PlantillaExcel Plantilla)
        {
            XSSFRow FilaTitulo = (XSSFRow)HojaActual.CreateRow(Plantilla.FilasCreadasIndice);

            int PosicionTitulo = 0;

            if (NumeroColumnas > 0)
            {
                PosicionTitulo = NumeroColumnas / 2;
            }
            var NuevaCelda = FilaTitulo.CreateCell(PosicionTitulo);

            if (Plantilla.EstiloTitulo != null)
            {
                NuevaCelda.CellStyle = GenerarCellStyleTitulo(workbook, Plantilla);
            }

            NuevaCelda.SetCellValue(Plantilla.TituloExcel);
            Plantilla.FilasCreadasIndice = 3;
        }
Esempio n. 8
0
 private static void GenerarNombreArchivoYContentType(ArchivoReporte resultado, PlantillaExcel Plantilla)
 {
     resultado.ContentType   = "application/vnd.ms-excel";
     resultado.NombreArchivo = string.Format("{0}.xlsx", Plantilla.NombreArchivo ?? "ReportExcel");
 }
Esempio n. 9
0
        private static void InsertarFilaDeDatos(XSSFWorkbook workbook, XSSFSheet HojaActual, PlantillaExcel Plantilla, PropertyInfo[] ColumnasDatos, object FilaDato)
        {
            int     ContadorColumna = 0;
            XSSFRow row             = (XSSFRow)HojaActual.CreateRow(Plantilla.FilasCreadasIndice);

            foreach (var itemCol in ColumnasDatos)
            {
                var strValue = string.Empty;

                if (itemCol.GetValue(FilaDato) != null)
                {
                    if (itemCol.PropertyType == typeof(string) || itemCol.PropertyType == typeof(int))
                    {
                        strValue = itemCol.GetValue(FilaDato).ToString();
                    }
                    else if (itemCol.PropertyType == typeof(DateTime))
                    {
                        strValue = DateTime.Parse(itemCol.GetValue(FilaDato).ToString()).ToShortDateString();
                    }
                    else if (itemCol.PropertyType == typeof(bool))
                    {
                        strValue = itemCol.GetValue(FilaDato).ToString().ToUpper() == "TRUE" ? "SI" : "NO";
                    }
                    else
                    {
                        strValue = itemCol.GetValue(FilaDato).ToString();
                    }
                }

                var NuevaCelda = row.CreateCell(ContadorColumna);
                if (Plantilla.EstiloCelda != null)
                {
                    NuevaCelda.CellStyle = GenerarCellStyleCelda(workbook, Plantilla);
                }

                NuevaCelda.SetCellValue(strValue);
                ContadorColumna++;
            }
            Plantilla.FilasCreadasIndice++;
        }