using ClosedXML.Excel; using System; using System.IO; using System.Linq; using AsbCloudApp.Services; namespace AsbCloudInfrastructure; public static class XLExtentions { public static IXLWorksheet GetWorksheet(this IXLWorkbook workbook, string sheetName) => workbook.Worksheets.FirstOrDefault(ws => string.Equals(ws.Name.Trim(), sheetName.Trim(), StringComparison.CurrentCultureIgnoreCase)) ?? throw new FileFormatException($"Книга excel не содержит листа {sheetName}."); public static IXLCell SetCellValue(this IXLCell cell, T value) { if (typeof(T) == typeof(DateTime)) cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS"; cell.Value = XLCellValue.FromObject(value); return cell; } public static IXLCell SetHyperlink(this IXLCell cell, string link) { cell.SetHyperlink(new XLHyperlink(link)); return cell; } public static T? GetCellValue(this IXLCell cell) { try { if (cell.IsEmpty() && default(T) == null) return default; return cell.GetValue(); } catch { var message = string.Format(IParserService.MessageTemplate, cell.Worksheet.Name, cell.Address.RowNumber, cell.Address.ColumnNumber, "Содержит некорректное значение"); throw new FileFormatException(message); } } }