| | 1 | | using Microsoft.Extensions.Configuration; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | | using MRA.DTO.Enums.Drawing; |
| | 4 | | using MRA.DTO.Models; |
| | 5 | | using MRA.Infrastructure.Excel; |
| | 6 | | using MRA.Infrastructure.Excel.Attributes; |
| | 7 | | using MRA.Infrastructure.Settings; |
| | 8 | | using MRA.Services.Excel.Interfaces; |
| | 9 | | using MRA.Services.Models.Drawings; |
| | 10 | | using OfficeOpenXml; |
| | 11 | | using OfficeOpenXml.Style; |
| | 12 | | using OfficeOpenXml.Table; |
| | 13 | | using System.Drawing; |
| | 14 | | using System.Reflection; |
| | 15 | |
|
| | 16 | | namespace MRA.Services.Excel; |
| | 17 | |
|
| | 18 | | public class ExcelService : IExcelService |
| | 19 | | { |
| | 20 | | public const string EXCEL_DRAWING_SHEET_NAME = "Drawings"; |
| | 21 | | public const string EXCEL_DRAWING_TABLE_NAME = "TableDrawings"; |
| | 22 | |
|
| | 23 | | public const string EXCEL_STYLE_SHEET_NAME = "Styles"; |
| | 24 | | public const string EXCEL_STYLE_TABLE_NAME = "TableStyles"; |
| | 25 | | public const string EXCEL_STYLE_COLUMN_NAME = "Type"; |
| | 26 | | public const string EXCEL_STYLE_COLUMN_INDEX = "#Type"; |
| | 27 | |
|
| | 28 | | public const string EXCEL_PRODUCT_SHEET_NAME = "Products"; |
| | 29 | | public const string EXCEL_PRODUCT_TABLE_NAME = "TableProducts"; |
| | 30 | | public const string EXCEL_PRODUCT_COLUMN_NAME = "Product Type"; |
| | 31 | | public const string EXCEL_PRODUCT_COLUMN_INDEX = "#Product Type"; |
| | 32 | |
|
| | 33 | | public const string EXCEL_SOFTWARE_SHEET_NAME = "Software"; |
| | 34 | | public const string EXCEL_SOFTWARE_TABLE_NAME = "TableSoftware"; |
| | 35 | | public const string EXCEL_SOFTWARE_COLUMN_NAME = "Software"; |
| | 36 | | public const string EXCEL_SOFTWARE_COLUMN_INDEX = "#Software"; |
| | 37 | |
|
| | 38 | | public const string EXCEL_PAPER_SHEET_NAME = "Papers"; |
| | 39 | | public const string EXCEL_PAPER_TABLE_NAME = "TablePapers"; |
| | 40 | | public const string EXCEL_PAPER_COLUMN_NAME = "Paper"; |
| | 41 | | public const string EXCEL_PAPER_COLUMN_INDEX = "#Paper"; |
| | 42 | |
|
| | 43 | | public const string EXCEL_FILTER_SHEET_NAME = "Filters"; |
| | 44 | | public const string EXCEL_FILTER_TABLE_NAME = "TableFilters"; |
| | 45 | | public const string EXCEL_FILTER_COLUMN_NAME = "Filter"; |
| | 46 | | public const string EXCEL_FILTER_COLUMN_INDEX = "#Filter"; |
| | 47 | |
|
| | 48 | | public const string EXCEL_FAVORITE_VALUE = "Favorite"; |
| | 49 | | public const string EXCEL_VISIBLE_VALUE = "Visible"; |
| | 50 | | public const string EXCEL_SEPARATOR_COMMENTS = "\n"; |
| | 51 | |
|
| 0 | 52 | | public string FilePath { get { return _appConfiguration.EPPlus.File.Path; } } |
| 0 | 53 | | public string FileName { get { return _appConfiguration.EPPlus.File.Name; } } |
| 0 | 54 | | public string FileDateFormat { get { return _appConfiguration.EPPlus.File.DateFormat; } } |
| 0 | 55 | | public string FileExtension { get { return _appConfiguration.EPPlus.File.Extension; } } |
| 0 | 56 | | public bool SaveFileLocally { get { return FilePath != null && FileName != null; } } |
| | 57 | |
|
| | 58 | | private readonly AppSettings _appConfiguration; |
| | 59 | | private readonly ILogger<ExcelService>? _logger; |
| | 60 | | private readonly IExcelProvider _excel; |
| | 61 | |
|
| | 62 | |
|
| 1 | 63 | | public ExcelService( |
| 1 | 64 | | AppSettings appConfig, |
| 1 | 65 | | ILogger<ExcelService> logger, |
| 1 | 66 | | IExcelProvider excelProvider) |
| | 67 | | { |
| 1 | 68 | | _appConfiguration = appConfig; |
| 1 | 69 | | _logger = logger; |
| 1 | 70 | | _excel = excelProvider; |
| 1 | 71 | | } |
| | 72 | |
|
| | 73 | |
|
| | 74 | |
|
| | 75 | | public void FillDrawingTable(ref ExcelWorksheet workSheet, List<ExcelColumnInfo> properties, List<DrawingModel> list |
| | 76 | | { |
| 0 | 77 | | _excel.SetTableHeaders(ref workSheet, properties); |
| 0 | 78 | | int numberDocuments = listDrawings.Count; |
| 0 | 79 | | int row = 2; |
| 0 | 80 | | foreach (var drawing in listDrawings) |
| | 81 | | { |
| 0 | 82 | | _logger?.LogInformation($"Procesando \"{drawing.Id}\" ({row - 1}/{numberDocuments})"); |
| 0 | 83 | | FillDrawingRow(ref workSheet, properties, drawing, row); |
| 0 | 84 | | row++; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | _excel.CreateTable(ref workSheet, EXCEL_DRAWING_TABLE_NAME, 1, 1, listDrawings.Count + 1, properties.Count); |
| 0 | 88 | | _excel.SetBold(ref workSheet, 2, 1, listDrawings.Count + 1, 1); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public void FillDrawingRow(ref ExcelWorksheet workSheet, List<ExcelColumnInfo> properties, DrawingModel drawing, int |
| | 92 | | { |
| 0 | 93 | | int col = 1; |
| 0 | 94 | | foreach (var prop in properties) |
| | 95 | | { |
| 0 | 96 | | workSheet.Column(col).Width = prop.Attribute.Width; |
| 0 | 97 | | var cell = workSheet.Cells[row, col]; |
| 0 | 98 | | object value = prop.GetValue(drawing); |
| 0 | 99 | | cell.Value = value; |
| | 100 | |
|
| 0 | 101 | | if(value is bool valueBool) |
| | 102 | | { |
| 0 | 103 | | switch (prop.Attribute.Name) |
| | 104 | | { |
| | 105 | | case "Visible": |
| 0 | 106 | | cell.Value = (valueBool ? EXCEL_VISIBLE_VALUE : "Oculto"); |
| 0 | 107 | | break; |
| | 108 | | case "Favorite": |
| 0 | 109 | | cell.Value = (valueBool ? EXCEL_FAVORITE_VALUE : ""); |
| 0 | 110 | | break; |
| | 111 | | default: |
| 0 | 112 | | cell.Value = (valueBool ? "TRUE" : "FALSE"); |
| 0 | 113 | | break; |
| | 114 | | } |
| | 115 | | } |
| 0 | 116 | | else if (value is double || value is float || value is decimal) |
| | 117 | | { |
| 0 | 118 | | cell.Style.Numberformat.Format = "#,##0.00"; |
| | 119 | | } |
| 0 | 120 | | else if (value is int) |
| | 121 | | { |
| 0 | 122 | | cell.Style.Numberformat.Format = "#,##0"; |
| | 123 | | } |
| 0 | 124 | | else if (value is DateTime) |
| | 125 | | { |
| 0 | 126 | | cell.Style.Numberformat.Format = "yyyy/mm/dd"; |
| | 127 | | } |
| 0 | 128 | | else if (value is List<string> stringList) |
| | 129 | | { |
| 0 | 130 | | switch (prop.Attribute.Name) |
| | 131 | | { |
| | 132 | | case "Tags": |
| 0 | 133 | | cell.Value = String.Join(DrawingTagManager.TAG_SEPARATOR, stringList); |
| 0 | 134 | | break; |
| | 135 | | default: |
| 0 | 136 | | cell.Value = String.Join(EXCEL_SEPARATOR_COMMENTS, stringList); |
| | 137 | | break; |
| | 138 | | } |
| 0 | 139 | | cell.Style.WrapText = true; |
| 0 | 140 | | cell.Style.VerticalAlignment = ExcelVerticalAlignment.Top; |
| 0 | 141 | | workSheet.Row(row).Height = 60; |
| | 142 | | } |
| 0 | 143 | | if (prop.Attribute.URL && value is string valueUrl && !String.IsNullOrEmpty(valueUrl)) |
| | 144 | | { |
| 0 | 145 | | cell.Hyperlink = new Uri(valueUrl); |
| 0 | 146 | | cell.Style.Font.UnderLine = true; |
| 0 | 147 | | cell.Style.Font.Color.SetColor(Color.Blue); |
| | 148 | | } |
| 0 | 149 | | if (prop.Attribute.WrapText) |
| | 150 | | { |
| 0 | 151 | | cell.Style.WrapText = true; |
| | 152 | | } |
| 0 | 153 | | col++; |
| | 154 | | } |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | |
|
| | 158 | |
|
| | 159 | | public void FillSheetsDictionary(ExcelPackage excel, List<ExcelColumnInfo> drawingProperties, ExcelWorksheet workShe |
| | 160 | | { |
| 0 | 161 | | _excel.CreateWorksheetDictionary<DrawingTypes>( |
| 0 | 162 | | excel, |
| 0 | 163 | | sheetName: EXCEL_STYLE_SHEET_NAME, |
| 0 | 164 | | tableName: EXCEL_STYLE_TABLE_NAME, |
| 0 | 165 | | drawingProperties, workSheet, |
| 0 | 166 | | nameColumnDropdown: EXCEL_STYLE_COLUMN_NAME, |
| 0 | 167 | | nameColumnIndex: EXCEL_STYLE_COLUMN_INDEX); |
| 0 | 168 | | _excel.CreateWorksheetDictionary<DrawingProductTypes>( |
| 0 | 169 | | excel, |
| 0 | 170 | | sheetName: EXCEL_PRODUCT_SHEET_NAME, |
| 0 | 171 | | tableName: EXCEL_PRODUCT_TABLE_NAME, |
| 0 | 172 | | drawingProperties, workSheet, |
| 0 | 173 | | nameColumnDropdown: EXCEL_PRODUCT_COLUMN_NAME, |
| 0 | 174 | | nameColumnIndex: EXCEL_PRODUCT_COLUMN_INDEX); |
| 0 | 175 | | _excel.CreateWorksheetDictionary<DrawingSoftwares>( |
| 0 | 176 | | excel, |
| 0 | 177 | | sheetName: EXCEL_SOFTWARE_SHEET_NAME, |
| 0 | 178 | | tableName: EXCEL_SOFTWARE_TABLE_NAME, |
| 0 | 179 | | drawingProperties, workSheet, |
| 0 | 180 | | nameColumnDropdown: EXCEL_SOFTWARE_COLUMN_NAME, |
| 0 | 181 | | nameColumnIndex: EXCEL_SOFTWARE_COLUMN_INDEX); |
| 0 | 182 | | _excel.CreateWorksheetDictionary<DrawingPaperSizes>( |
| 0 | 183 | | excel, |
| 0 | 184 | | sheetName: EXCEL_PAPER_SHEET_NAME, |
| 0 | 185 | | tableName: EXCEL_PAPER_TABLE_NAME, |
| 0 | 186 | | drawingProperties, workSheet, |
| 0 | 187 | | nameColumnDropdown: EXCEL_PAPER_COLUMN_NAME, |
| 0 | 188 | | nameColumnIndex: EXCEL_PAPER_COLUMN_INDEX); |
| 0 | 189 | | _excel.CreateWorksheetDictionary<DrawingFilterTypes>( |
| 0 | 190 | | excel, |
| 0 | 191 | | sheetName: EXCEL_FILTER_SHEET_NAME, |
| 0 | 192 | | tableName: EXCEL_FILTER_TABLE_NAME, |
| 0 | 193 | | drawingProperties, workSheet, |
| 0 | 194 | | nameColumnDropdown: EXCEL_FILTER_COLUMN_NAME, |
| 0 | 195 | | nameColumnIndex: EXCEL_FILTER_COLUMN_INDEX); |
| 0 | 196 | | } |
| | 197 | |
|
| 0 | 198 | | public string GetFileName() => $"{FileName}" + $"_{DateTime.Now.ToString(FileDateFormat)}" + $".{FileExtension}"; |
| | 199 | |
|
| | 200 | | public FileInfo GetFileInfo() |
| | 201 | | { |
| 0 | 202 | | var fileName = GetFileName(); |
| 0 | 203 | | var filePath = Path.Combine(FilePath, fileName); |
| | 204 | |
|
| 0 | 205 | | if (!Directory.Exists(FilePath)) |
| | 206 | | { |
| 0 | 207 | | Directory.CreateDirectory(FilePath); |
| | 208 | | } |
| | 209 | |
|
| 0 | 210 | | return new FileInfo(filePath); |
| | 211 | | } |
| | 212 | |
|
| | 213 | | public List<ExcelColumnInfo> GetPropertiesAttributes<T>() |
| | 214 | | { |
| 0 | 215 | | return _excel.GetPropertiesAttributes<T>(); |
| | 216 | | } |
| | 217 | |
|
| 0 | 218 | | public Dictionary<string, int> GetColumnMapDrawing(ExcelWorksheet workSheet) => _excel.GetColumnMapDrawing(workSheet |
| | 219 | | } |