using ClosedXML.Excel; using System; using System.IO; using System.Linq; 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 SetCellValue(this IXLCell cell, T value, string format) { if (typeof(T) == typeof(DateTime)) cell.Style.DateFormat.Format = format; 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 { throw new FileFormatException( $"Лист '{cell.Worksheet.Name}'. {cell.Address.RowNumber} строка содержит некорректное значение в {cell.Address.ColumnNumber} столбце"); } } }