forked from ddrilling/AsbCloudServer
108644c13d
1. Почистил методы расширения для ячеек 2. Добавил методы расширения для парсинга
96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
using ClosedXML.Excel;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
|
|
namespace AsbCloudInfrastructure;
|
|
|
|
internal static class XLExtentions
|
|
{
|
|
internal static IXLCell _SetValue(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS", bool setAllBorders = true)
|
|
{
|
|
cell.Value = value;
|
|
if (setAllBorders == true)
|
|
{
|
|
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;
|
|
}
|
|
|
|
public static IXLCell SetVal(this IXLCell cell, double? value, string format = "0.00")
|
|
{
|
|
cell.Value = (value is not null && double.IsFinite(value.Value)) ? value : null;
|
|
cell.DataType = XLDataType.Number;
|
|
cell.Style.NumberFormat.Format = format;
|
|
return cell;
|
|
}
|
|
|
|
public static IXLCell SetVal(this IXLCell cell, string value, bool adaptRowHeight = false)
|
|
{
|
|
cell.Value = value;
|
|
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;
|
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
|
}
|
|
}
|
|
|
|
return cell;
|
|
}
|
|
|
|
public static IXLCell SetVal(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS")
|
|
{
|
|
cell.Value = value;
|
|
cell.DataType = XLDataType.DateTime;
|
|
cell.Style.DateFormat.Format = dateFormat;
|
|
|
|
return cell;
|
|
}
|
|
|
|
private 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;
|
|
}
|
|
|
|
internal static T? GetCellValue<T>(this IXLCell cell)
|
|
{
|
|
try
|
|
{
|
|
if (cell.IsEmpty() && default(T) == null)
|
|
return default;
|
|
|
|
if (typeof(T) != typeof(DateTime))
|
|
return (T)Convert.ChangeType(cell.GetFormattedString(), typeof(T), CultureInfo.InvariantCulture);
|
|
|
|
if (cell.Value is DateTime dateTime)
|
|
return (T)(object)dateTime;
|
|
|
|
return (T)(object)DateTime.FromOADate((double)cell.Value);
|
|
}
|
|
catch
|
|
{
|
|
throw new FileFormatException(
|
|
$"Лист '{cell.Worksheet.Name}'. Ячейка: ({cell.Address.RowNumber},{cell.Address.ColumnNumber}) содержит некорректное значение");
|
|
}
|
|
}
|
|
} |