public bool GenerarArchivo(ExcelMetadata excelMetadata, out string filePath)
        {
            try
            {
                if (File.Exists(fileInfo.FullName))
                {
                    File.Delete(fileInfo.FullName);
                }

                using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
                {
                    foreach (ExcelTab tab in excelMetadata.Tabs)
                    {
                        ExcelWorksheet ws = excelPackage.Workbook.Worksheets.Add(tab.Nombre);

                        if (tab.Filas != null && tab.Filas.Count > 0)
                        {
                            var properties = tab.Filas[0].GetType().GetProperties();

                            foreach (PropertyInfo property in properties)
                            {
                                ExcelColumnDescAttribute columnMeta = (ExcelColumnDescAttribute)property.GetCustomAttributes(typeof(ExcelColumnDescAttribute), true).FirstOrDefault();
                                if (columnMeta != null)
                                {
                                    var        columnLetter = columnMeta.Column;
                                    var        headerCell   = ws.Cells[columnLetter + 1];
                                    int        rowData      = 2;
                                    bool       ultimoRow    = false;
                                    ExcelRange dataCell     = null;

                                    headerCell.Value = columnMeta.Header ?? property.Name.SplitCamelCase();
                                    foreach (var row in tab.Filas)
                                    {
                                        dataCell = ws.Cells[columnLetter + rowData];
                                        if ((rowData - 1) == tab.Filas.Count)
                                        {
                                            ultimoRow      = true;
                                            dataCell.Value = "";
                                            rowData++;
                                        }

                                        dataCell       = ws.Cells[columnLetter + rowData];
                                        dataCell.Value = property.GetValue(row);

                                        if (ultimoRow && columnLetter == "A")
                                        {
                                            dataCell.Value = "Totales";
                                        }

                                        if (columnMeta.Type == TypeDataExcel.Decimal)
                                        {
                                            if (property.GetValue(row) != null)
                                            {
                                                if ((decimal)property.GetValue(row) < 0)
                                                {
                                                    dataCell.Style.Font.Color.SetColor(Color.Red);
                                                }
                                            }
                                            dataCell.Style.Numberformat.Format = "#,##0.00_-";
                                        }
                                        rowData++;
                                    }
                                }
                            }
                        }
                    }
                    if (!esTemplate)
                    {
                        excelPackage.Save();
                    }
                    else
                    {
                        FileInfo newFile = new FileInfo(fileInfo.FullName);
                        excelPackage.SaveAs(newFile);
                    }
                }

                filePath = fileInfo.FullName;
            }
            catch
            {
                filePath = fileInfo.FullName;
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
 public ExcelDef(string consulta)
 {
     Metadato = new ExcelMetadata();
     Metadato.AgregarTabs(ObtenerTabs(consulta));
 }