2022-06-23 18:04:01 +05:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.DailyReport
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-06-23 18:04:01 +05:00
|
|
|
|
internal static class XLExtentions
|
|
|
|
|
{
|
2022-06-24 11:41:46 +05:00
|
|
|
|
public static IXLRange _SetValue(this IXLRange range, object value)
|
2022-06-23 18:04:01 +05:00
|
|
|
|
{
|
2022-07-06 15:04:03 +05:00
|
|
|
|
var mergedRange = range.Merge();
|
2022-06-24 11:41:46 +05:00
|
|
|
|
mergedRange.FirstCell()._SetValue(value);
|
2022-07-06 15:04:03 +05:00
|
|
|
|
var colWidth = mergedRange.FirstCell().WorksheetColumn().Width;
|
2022-09-29 12:29:10 +05:00
|
|
|
|
var maxCharsToWrap = colWidth / (0.1d * mergedRange.FirstCell().Style.Font.FontSize);
|
2022-07-06 15:04:03 +05:00
|
|
|
|
if (value is string valueString && valueString.Length > maxCharsToWrap)
|
|
|
|
|
{
|
|
|
|
|
var row = mergedRange.FirstCell().WorksheetRow();
|
|
|
|
|
var baseHeight = row.Height;
|
2022-09-29 12:29:10 +05:00
|
|
|
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + valueString.Length / maxCharsToWrap);
|
2022-07-06 15:04:03 +05:00
|
|
|
|
}
|
|
|
|
|
mergedRange.Style.SetAllBorders()
|
|
|
|
|
.Alignment.SetWrapText(true);
|
2022-06-24 11:41:46 +05:00
|
|
|
|
return mergedRange;
|
2022-06-23 18:04:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 11:41:46 +05:00
|
|
|
|
public static IXLCell _SetValue(this IXLCell cell, object value)
|
2022-06-23 18:04:01 +05:00
|
|
|
|
{
|
2022-06-23 18:10:33 +05:00
|
|
|
|
switch (value)
|
2022-06-23 18:04:01 +05:00
|
|
|
|
{
|
2022-06-23 18:10:33 +05:00
|
|
|
|
case DateTime dateTime:
|
2022-06-24 11:41:46 +05:00
|
|
|
|
cell._SetValue(dateTime);
|
2022-06-23 18:10:33 +05:00
|
|
|
|
break;
|
|
|
|
|
case IFormattable formattable:
|
2022-06-24 11:41:46 +05:00
|
|
|
|
cell._SetValue(formattable);
|
2022-06-23 18:10:33 +05:00
|
|
|
|
break;
|
|
|
|
|
case string valueString:
|
2022-06-24 11:41:46 +05:00
|
|
|
|
cell._SetValue(valueString);
|
2022-06-23 18:10:33 +05:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
break;
|
2022-06-23 18:04:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 15:04:03 +05:00
|
|
|
|
public static IXLCell _SetValue(this IXLCell cell, string value, bool adaptRowHeight = false)
|
2022-06-23 18:04:01 +05:00
|
|
|
|
{
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
cell.Style
|
|
|
|
|
.SetAllBorders()
|
|
|
|
|
.Alignment.WrapText = true;
|
|
|
|
|
|
|
|
|
|
cell.Value = value;
|
2022-07-06 15:04:03 +05:00
|
|
|
|
if (adaptRowHeight)
|
|
|
|
|
{
|
|
|
|
|
var colWidth = cell.WorksheetColumn().Width;
|
|
|
|
|
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
|
|
|
|
if (value.Length > maxCharsToWrap)
|
|
|
|
|
{
|
|
|
|
|
var row = cell.WorksheetRow();
|
|
|
|
|
var baseHeight = row.Height;
|
2022-09-29 12:29:10 +05:00
|
|
|
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
2022-07-06 15:04:03 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
2022-06-23 18:04:01 +05:00
|
|
|
|
|
2022-07-06 15:04:03 +05:00
|
|
|
|
public static IXLCell _ValueNoBorder(this IXLCell cell, string value, bool adaptRowHeight = false)
|
|
|
|
|
{
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
cell.Style.Alignment.WrapText = true;
|
|
|
|
|
|
|
|
|
|
cell.Value = value;
|
2022-06-23 18:04:01 +05:00
|
|
|
|
if (adaptRowHeight)
|
|
|
|
|
{
|
2022-07-06 09:21:35 +05:00
|
|
|
|
var colWidth = cell.WorksheetColumn().Width;
|
2022-07-06 15:04:03 +05:00
|
|
|
|
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
2022-07-06 09:21:35 +05:00
|
|
|
|
if (value.Length > maxCharsToWrap)
|
2022-06-23 18:04:01 +05:00
|
|
|
|
{
|
|
|
|
|
var row = cell.WorksheetRow();
|
|
|
|
|
var baseHeight = row.Height;
|
2022-09-29 12:29:10 +05:00
|
|
|
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
2022-06-23 18:04:01 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
2022-07-06 15:04:03 +05:00
|
|
|
|
|
2022-07-06 09:21:35 +05:00
|
|
|
|
|
|
|
|
|
|
2022-06-23 18:04:01 +05:00
|
|
|
|
|
2022-06-24 11:41:46 +05:00
|
|
|
|
public static IXLCell _SetValue(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS")
|
2022-06-23 18:10:33 +05:00
|
|
|
|
{
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
cell.Style
|
|
|
|
|
.SetAllBorders()
|
|
|
|
|
.Alignment.WrapText = true;
|
|
|
|
|
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
|
|
|
|
|
cell.DataType = XLDataType.DateTime;
|
|
|
|
|
cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS";
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 11:41:46 +05:00
|
|
|
|
public static IXLCell _SetValue(this IXLCell cell, IFormattable value, string format = "0.00")
|
2022-06-23 18:10:33 +05:00
|
|
|
|
{
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
cell.Style
|
|
|
|
|
.SetAllBorders()
|
|
|
|
|
.Alignment.WrapText = true;
|
|
|
|
|
|
|
|
|
|
cell.Value = value;
|
|
|
|
|
|
|
|
|
|
cell.DataType = XLDataType.Number;
|
|
|
|
|
cell.Style.NumberFormat.Format = "0.00";
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-23 18:04:01 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
2022-06-24 11:41:46 +05:00
|
|
|
|
|
|
|
|
|
public static IXLStyle SetBaseFont(this IXLStyle style)
|
|
|
|
|
{
|
|
|
|
|
style.Font.FontName = "Calibri";
|
|
|
|
|
style.Font.FontSize = 10;
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IXLStyle SetH1(this IXLStyle style)
|
|
|
|
|
{
|
|
|
|
|
style.Font.FontName = "Calibri";
|
|
|
|
|
style.Font.FontSize = 14;
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Костыль исправляющий проблему в библиотеке IXLRange Range(this IXLWorksheet, IXLAddress, IXLAddress) с кастингом IXLAddress к XLAddress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sheet"></param>
|
|
|
|
|
/// <param name="begin"></param>
|
|
|
|
|
/// <param name="end"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IXLRange _Range(this IXLWorksheet sheet, CellAddress begin, CellAddress end)
|
|
|
|
|
=> sheet.Range(begin.RowNumber, begin.ColumnNumber, end.RowNumber, end.ColumnNumber);
|
2022-07-06 09:21:35 +05:00
|
|
|
|
|
2022-06-23 18:04:01 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-06-23 18:04:01 +05:00
|
|
|
|
}
|