using ClosedXML.Excel; using System; namespace AsbCloudInfrastructure.Services.DailyReport { internal static class XLExtentions { public static IXLRange SetValue(this IXLRange range, object value, int maxCharsToWrap = 88) { range.Merge(); range.FirstCell().SetValue(value, maxCharsToWrap); return range; } public static IXLCell SetValue(this IXLCell cell, object value) { cell.Value = value; cell.Style .SetAllBorders() .Alignment.WrapText = true; if (value is string valueString && valueString.Length > maxChartsToWrap) { var row = cell.WorksheetRow(); var baseHeight = row.Height; row.Height = 0.82d * baseHeight * Math.Ceiling(1d + valueString.Length / maxChartsToWrap); } if (value is DateTime) { cell.DataType = XLDataType.DateTime; cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS"; } else if (value is IFormattable) { cell.DataType = XLDataType.Number; cell.Style.NumberFormat.Format = "0.00"; } return cell; } public static IXLCell SetValue(this IXLCell cell, string value, bool adaptRowHeight = false) { cell.Value = value; cell.Style .SetAllBorders() .Alignment.WrapText = true; cell.Value = value; if (adaptRowHeight) { var colWidth = cell.WorksheetColumn().Width; var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize); // TODO: Подобрать коэффициент if(value.Length > maxCharsToWrap) { var row = cell.WorksheetRow(); var baseHeight = row.Height; row.Height = 0.82d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap); } } return cell; } public static IXLStyle SetAllBorders(this IXLStyle style, XLBorderStyleValues borderStyle = XLBorderStyleValues.Thin) { style.Border.RightBorder = borderStyle; style.Border.LeftBorder = borderStyle; style.Border.TopBorder = borderStyle; style.Border.BottomBorder = borderStyle; style.Border.InsideBorder = borderStyle; style.Border.OutsideBorder = borderStyle; return style; } } }