forked from ddrilling/AsbCloudServer
Форматирование файла
This commit is contained in:
parent
50e8f557d8
commit
85086e7206
@ -8,123 +8,123 @@ namespace AsbCloudInfrastructure;
|
|||||||
|
|
||||||
internal static class XLExtentions
|
internal static class XLExtentions
|
||||||
{
|
{
|
||||||
internal static IXLRange _SetValue(this IXLRange range, object value)
|
internal static IXLRange _SetValue(this IXLRange range, object value)
|
||||||
{
|
{
|
||||||
var mergedRange = range.Merge();
|
var mergedRange = range.Merge();
|
||||||
mergedRange.FirstCell()._SetValue(value);
|
mergedRange.FirstCell()._SetValue(value);
|
||||||
var colWidth = mergedRange.FirstCell().WorksheetColumn().Width;
|
var colWidth = mergedRange.FirstCell().WorksheetColumn().Width;
|
||||||
var maxCharsToWrap = colWidth / (0.1d * mergedRange.FirstCell().Style.Font.FontSize);
|
var maxCharsToWrap = colWidth / (0.1d * mergedRange.FirstCell().Style.Font.FontSize);
|
||||||
if (value is string valueString && valueString.Length > maxCharsToWrap)
|
if (value is string valueString && valueString.Length > maxCharsToWrap)
|
||||||
{
|
{
|
||||||
var row = mergedRange.FirstCell().WorksheetRow();
|
var row = mergedRange.FirstCell().WorksheetRow();
|
||||||
var baseHeight = row.Height;
|
var baseHeight = row.Height;
|
||||||
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + valueString.Length / maxCharsToWrap);
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + valueString.Length / maxCharsToWrap);
|
||||||
}
|
}
|
||||||
|
|
||||||
mergedRange.Style.SetAllBorders()
|
mergedRange.Style.SetAllBorders()
|
||||||
.Alignment.SetWrapText(true);
|
.Alignment.SetWrapText(true);
|
||||||
return mergedRange;
|
return mergedRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLCell _SetValue(this IXLCell cell, object value)
|
internal static IXLCell _SetValue(this IXLCell cell, object value)
|
||||||
{
|
{
|
||||||
switch (value)
|
switch (value)
|
||||||
{
|
{
|
||||||
case DateTime dateTime:
|
case DateTime dateTime:
|
||||||
cell._SetValue(dateTime);
|
cell._SetValue(dateTime);
|
||||||
break;
|
break;
|
||||||
case IFormattable formattable:
|
case IFormattable formattable:
|
||||||
cell._SetValue(formattable);
|
cell._SetValue(formattable);
|
||||||
break;
|
break;
|
||||||
case string valueString:
|
case string valueString:
|
||||||
cell._SetValue(valueString);
|
cell._SetValue(valueString);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLCell _SetValue(this IXLCell cell, string value, bool adaptRowHeight = false)
|
internal static IXLCell _SetValue(this IXLCell cell, string value, bool adaptRowHeight = false)
|
||||||
{
|
{
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
cell.Style
|
cell.Style
|
||||||
.SetAllBorders()
|
.SetAllBorders()
|
||||||
.Alignment.WrapText = true;
|
.Alignment.WrapText = true;
|
||||||
|
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
if (adaptRowHeight)
|
if (adaptRowHeight)
|
||||||
{
|
{
|
||||||
var colWidth = cell.WorksheetColumn().Width;
|
var colWidth = cell.WorksheetColumn().Width;
|
||||||
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
||||||
if (value.Length > maxCharsToWrap)
|
if (value.Length > maxCharsToWrap)
|
||||||
{
|
{
|
||||||
var row = cell.WorksheetRow();
|
var row = cell.WorksheetRow();
|
||||||
var baseHeight = row.Height;
|
var baseHeight = row.Height;
|
||||||
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLCell _ValueNoBorder(this IXLCell cell, string value, bool adaptRowHeight = false)
|
internal static IXLCell _ValueNoBorder(this IXLCell cell, string value, bool adaptRowHeight = false)
|
||||||
{
|
{
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
cell.Style.Alignment.WrapText = true;
|
cell.Style.Alignment.WrapText = true;
|
||||||
|
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
if (adaptRowHeight)
|
if (adaptRowHeight)
|
||||||
{
|
{
|
||||||
var colWidth = cell.WorksheetColumn().Width;
|
var colWidth = cell.WorksheetColumn().Width;
|
||||||
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
var maxCharsToWrap = colWidth / (0.1d * cell.Style.Font.FontSize);
|
||||||
if (value.Length > maxCharsToWrap)
|
if (value.Length > maxCharsToWrap)
|
||||||
{
|
{
|
||||||
var row = cell.WorksheetRow();
|
var row = cell.WorksheetRow();
|
||||||
var baseHeight = row.Height;
|
var baseHeight = row.Height;
|
||||||
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
row.Height = 0.5d * baseHeight * Math.Ceiling(1d + value.Length / maxCharsToWrap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal static IXLCell _SetValue(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS", bool? setAllBorders = true)
|
internal static IXLCell _SetValue(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS", bool? setAllBorders = true)
|
||||||
{
|
{
|
||||||
cell.Value = value;
|
cell.Value = value;
|
||||||
if (setAllBorders == true)
|
if (setAllBorders == true)
|
||||||
{
|
{
|
||||||
cell.Style
|
cell.Style
|
||||||
.SetAllBorders()
|
.SetAllBorders()
|
||||||
.Alignment.WrapText = true;
|
.Alignment.WrapText = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cell.Value = value;
|
|
||||||
|
|
||||||
cell.DataType = XLDataType.DateTime;
|
cell.Value = value;
|
||||||
cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS";
|
|
||||||
|
|
||||||
return cell;
|
cell.DataType = XLDataType.DateTime;
|
||||||
}
|
cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS";
|
||||||
|
|
||||||
internal static IXLCell _SetValue(this IXLCell cell, IFormattable value, string format = "0.00")
|
return cell;
|
||||||
{
|
}
|
||||||
cell.Value = value;
|
|
||||||
cell.Style
|
|
||||||
.SetAllBorders()
|
|
||||||
.Alignment.WrapText = true;
|
|
||||||
|
|
||||||
cell.Value = value;
|
internal static IXLCell _SetValue(this IXLCell cell, IFormattable value, string format = "0.00")
|
||||||
|
{
|
||||||
|
cell.Value = value;
|
||||||
|
cell.Style
|
||||||
|
.SetAllBorders()
|
||||||
|
.Alignment.WrapText = true;
|
||||||
|
|
||||||
cell.DataType = XLDataType.Number;
|
cell.Value = value;
|
||||||
cell.Style.NumberFormat.Format = "0.00";
|
|
||||||
|
|
||||||
return cell;
|
cell.DataType = XLDataType.Number;
|
||||||
}
|
cell.Style.NumberFormat.Format = "0.00";
|
||||||
|
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
public static IXLCell SetVal(this IXLCell cell, double? value, string format = "0.00")
|
public static IXLCell SetVal(this IXLCell cell, double? value, string format = "0.00")
|
||||||
{
|
{
|
||||||
@ -162,60 +162,60 @@ internal static class XLExtentions
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLStyle SetAllBorders(this IXLStyle style, XLBorderStyleValues borderStyle = XLBorderStyleValues.Thin)
|
internal static IXLStyle SetAllBorders(this IXLStyle style, XLBorderStyleValues borderStyle = XLBorderStyleValues.Thin)
|
||||||
{
|
{
|
||||||
style.Border.RightBorder = borderStyle;
|
style.Border.RightBorder = borderStyle;
|
||||||
style.Border.LeftBorder = borderStyle;
|
style.Border.LeftBorder = borderStyle;
|
||||||
style.Border.TopBorder = borderStyle;
|
style.Border.TopBorder = borderStyle;
|
||||||
style.Border.BottomBorder = borderStyle;
|
style.Border.BottomBorder = borderStyle;
|
||||||
style.Border.InsideBorder = borderStyle;
|
style.Border.InsideBorder = borderStyle;
|
||||||
style.Border.OutsideBorder = borderStyle;
|
style.Border.OutsideBorder = borderStyle;
|
||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLStyle SetBaseFont(this IXLStyle style)
|
internal static IXLStyle SetBaseFont(this IXLStyle style)
|
||||||
{
|
{
|
||||||
style.Font.FontName = "Calibri";
|
style.Font.FontName = "Calibri";
|
||||||
style.Font.FontSize = 10;
|
style.Font.FontSize = 10;
|
||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static IXLStyle SetH1(this IXLStyle style)
|
internal static IXLStyle SetH1(this IXLStyle style)
|
||||||
{
|
{
|
||||||
style.Font.FontName = "Calibri";
|
style.Font.FontName = "Calibri";
|
||||||
style.Font.FontSize = 14;
|
style.Font.FontSize = 14;
|
||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Костыль исправляющий проблему в библиотеке IXLRange Range(this IXLWorksheet, IXLAddress, IXLAddress) с кастингом IXLAddress к XLAddress.
|
/// Костыль исправляющий проблему в библиотеке IXLRange Range(this IXLWorksheet, IXLAddress, IXLAddress) с кастингом IXLAddress к XLAddress.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sheet"></param>
|
/// <param name="sheet"></param>
|
||||||
/// <param name="begin"></param>
|
/// <param name="begin"></param>
|
||||||
/// <param name="end"></param>
|
/// <param name="end"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal static IXLRange _Range(this IXLWorksheet sheet, CellAddress begin, CellAddress end)
|
internal static IXLRange _Range(this IXLWorksheet sheet, CellAddress begin, CellAddress end)
|
||||||
=> sheet.Range(begin.RowNumber, begin.ColumnNumber, end.RowNumber, end.ColumnNumber);
|
=> sheet.Range(begin.RowNumber, begin.ColumnNumber, end.RowNumber, end.ColumnNumber);
|
||||||
|
|
||||||
|
|
||||||
internal static T? GetCellValue<T>(this IXLCell cell)
|
internal static T? GetCellValue<T>(this IXLCell cell)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (cell.IsEmpty() && default(T) == null)
|
if (cell.IsEmpty() && default(T) == null)
|
||||||
return default;
|
return default;
|
||||||
|
|
||||||
if (typeof(T) != typeof(DateTime))
|
if (typeof(T) != typeof(DateTime))
|
||||||
return (T)Convert.ChangeType(cell.GetFormattedString(), typeof(T), CultureInfo.InvariantCulture);
|
return (T)Convert.ChangeType(cell.GetFormattedString(), typeof(T), CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
if (cell.Value is DateTime dateTime)
|
if (cell.Value is DateTime dateTime)
|
||||||
return (T)(object)dateTime;
|
return (T)(object)dateTime;
|
||||||
|
|
||||||
return (T)(object)DateTime.FromOADate((double)cell.Value);
|
return (T)(object)DateTime.FromOADate((double)cell.Value);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
throw new FileFormatException(
|
throw new FileFormatException(
|
||||||
$"Лист '{cell.Worksheet.Name}'. Ячейка: ({cell.Address.RowNumber},{cell.Address.ColumnNumber}) содержит некорректное значение");
|
$"Лист '{cell.Worksheet.Name}'. Ячейка: ({cell.Address.RowNumber},{cell.Address.ColumnNumber}) содержит некорректное значение");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user