From 108644c13d24e521d6342b5e4a8d630477701d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 31 Jan 2024 17:18:55 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D1=88=D0=B8=D1=80=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Почистил методы расширения для ячеек 2. Добавил методы расширения для парсинга --- AsbCloudInfrastructure/XLExtentions.cs | 116 +------------------ AsbCloudInfrastructure/XLParserExtensions.cs | 59 ++++++++++ 2 files changed, 60 insertions(+), 115 deletions(-) create mode 100644 AsbCloudInfrastructure/XLParserExtensions.cs diff --git a/AsbCloudInfrastructure/XLExtentions.cs b/AsbCloudInfrastructure/XLExtentions.cs index 3a583bf9..401e2423 100644 --- a/AsbCloudInfrastructure/XLExtentions.cs +++ b/AsbCloudInfrastructure/XLExtentions.cs @@ -2,96 +2,11 @@ using System; using System.Globalization; using System.IO; -using AsbCloudInfrastructure.Services.DailyReport; namespace AsbCloudInfrastructure; internal static class XLExtentions { - internal static IXLRange _SetValue(this IXLRange range, object value) - { - var mergedRange = range.Merge(); - mergedRange.FirstCell()._SetValue(value); - var colWidth = mergedRange.FirstCell().WorksheetColumn().Width; - var maxCharsToWrap = colWidth / (0.1d * mergedRange.FirstCell().Style.Font.FontSize); - if (value is string valueString && valueString.Length > maxCharsToWrap) - { - var row = mergedRange.FirstCell().WorksheetRow(); - var baseHeight = row.Height; - row.Height = 0.5d * baseHeight * Math.Ceiling(1d + valueString.Length / maxCharsToWrap); - } - - mergedRange.Style.SetAllBorders() - .Alignment.SetWrapText(true); - return mergedRange; - } - - internal static IXLCell _SetValue(this IXLCell cell, object value) - { - switch (value) - { - case DateTime dateTime: - cell._SetValue(dateTime); - break; - case IFormattable formattable: - cell._SetValue(formattable); - break; - case string valueString: - cell._SetValue(valueString); - break; - default: - cell.Value = value; - break; - } - - return cell; - } - - internal 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); - 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; - } - - internal static IXLCell _ValueNoBorder(this IXLCell cell, string value, bool adaptRowHeight = false) - { - cell.Value = value; - cell.Style.Alignment.WrapText = true; - - 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; - } - - internal static IXLCell _SetValue(this IXLCell cell, DateTime value, string dateFormat = "DD.MM.YYYY HH:MM:SS", bool setAllBorders = true) { cell.Value = value; @@ -111,21 +26,6 @@ internal static class XLExtentions return cell; } - internal static IXLCell _SetValue(this IXLCell cell, IFormattable value, string format = "0.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; - } - 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; @@ -161,7 +61,7 @@ internal static class XLExtentions return cell; } - internal static IXLStyle SetAllBorders(this IXLStyle style, XLBorderStyleValues borderStyle = XLBorderStyleValues.Thin) + private static IXLStyle SetAllBorders(this IXLStyle style, XLBorderStyleValues borderStyle = XLBorderStyleValues.Thin) { style.Border.RightBorder = borderStyle; style.Border.LeftBorder = borderStyle; @@ -172,20 +72,6 @@ internal static class XLExtentions return style; } - internal static IXLStyle SetBaseFont(this IXLStyle style) - { - style.Font.FontName = "Calibri"; - style.Font.FontSize = 10; - return style; - } - - internal static IXLStyle SetH1(this IXLStyle style) - { - style.Font.FontName = "Calibri"; - style.Font.FontSize = 14; - return style; - } - internal static T? GetCellValue(this IXLCell cell) { try diff --git a/AsbCloudInfrastructure/XLParserExtensions.cs b/AsbCloudInfrastructure/XLParserExtensions.cs new file mode 100644 index 00000000..28ecb089 --- /dev/null +++ b/AsbCloudInfrastructure/XLParserExtensions.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.IO; +using System.Linq; +using AsbCloudApp.Data; +using ClosedXML.Excel; + +namespace AsbCloudInfrastructure; + +public static class XLParserExtensions +{ + public static ParserResultDto Parse(this IXLWorksheet sheet, + Func> parseRow, + int columnCount, + int headerRowsCount = 0) + where TDto : class, IId + { + if (sheet.RangeUsed().RangeAddress.LastAddress.ColumnNumber < columnCount) + throw new FileFormatException($"Лист {sheet.Name} содержит меньшее количество столбцов."); + + var count = sheet.RowsUsed().Count() - headerRowsCount; + + if (count > 1024) + throw new FileFormatException($"Лист {sheet.Name} содержит слишком большое количество строк."); + + if (count <= 0) + return new ParserResultDto(); + + var dtos = new List>(count); + var warnings = new List(); + + for (var i = 0; i < count; i++) + { + var row = sheet.Row(1 + i + headerRowsCount); + + try + { + var dto = parseRow.Invoke(row); + dtos.Add(dto); + } + catch (FileFormatException ex) + { + var warning = new ValidationResult(ex.Message); + warnings.Add(warning); + } + } + + var parserResult = new ParserResultDto + { + Item = dtos + }; + + if (warnings.Any()) + parserResult.Warnings = warnings; + + return parserResult; + } +} \ No newline at end of file