diff --git a/Excel.TemplateEngine.Tests/Excel.TemplateEngine.Tests.csproj b/Excel.TemplateEngine.Tests/Excel.TemplateEngine.Tests.csproj index 9cef4ce..7b6507e 100644 --- a/Excel.TemplateEngine.Tests/Excel.TemplateEngine.Tests.csproj +++ b/Excel.TemplateEngine.Tests/Excel.TemplateEngine.Tests.csproj @@ -11,7 +11,7 @@ - + diff --git a/Excel.TemplateEngine.Tests/FileGeneratingTests/ExcelStyleTests.cs b/Excel.TemplateEngine.Tests/FileGeneratingTests/ExcelStyleTests.cs index 56a37be..89e998e 100644 --- a/Excel.TemplateEngine.Tests/FileGeneratingTests/ExcelStyleTests.cs +++ b/Excel.TemplateEngine.Tests/FileGeneratingTests/ExcelStyleTests.cs @@ -109,15 +109,16 @@ public void CellBorderFormatExtractionTest() style.BordersStyle.BottomBorder.Color.Blue.Should().Be(255); } - [Test] - public void CellAlignmentExtractionTest() + [TestCase(ExcelHorizontalAlignment.Left, "A1")] + [TestCase(ExcelHorizontalAlignment.Fill, "B1")] + public void CellAlignmentExtractionTest(ExcelHorizontalAlignment horizontalAlignment, string cellIndex) { var templateDocument = ExcelDocumentFactory.CreateFromTemplate(File.ReadAllBytes(GetFilePath("template.xlsx")), logger); - var cell = templateDocument.GetWorksheet(0).GetCell(new ExcelCellIndex("A1")); + var cell = templateDocument.GetWorksheet(0).GetCell(new ExcelCellIndex(cellIndex)); var style = cell.GetStyle(); style.Alignment.WrapText.Should().BeTrue(); - style.Alignment.HorizontalAlignment.Should().Be(ExcelHorizontalAlignment.Left); + style.Alignment.HorizontalAlignment.Should().Be(horizontalAlignment); } [Test] diff --git a/Excel.TemplateEngine.Tests/FileGeneratingTests/Files/template.xlsx b/Excel.TemplateEngine.Tests/FileGeneratingTests/Files/template.xlsx index ef83af1..eb7c774 100644 Binary files a/Excel.TemplateEngine.Tests/FileGeneratingTests/Files/template.xlsx and b/Excel.TemplateEngine.Tests/FileGeneratingTests/Files/template.xlsx differ diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/complexTemplate.xlsx b/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/complexTemplate.xlsx index d41266b..ae2cd02 100644 Binary files a/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/complexTemplate.xlsx and b/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/complexTemplate.xlsx differ diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectExcelPrintingTests.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectExcelPrintingTests.cs index 47df763..edea614 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectExcelPrintingTests.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectExcelPrintingTests.cs @@ -119,7 +119,7 @@ public void PrintComplexObjectTest() }, DeliveryParty = new Organization { - Address = "DeliveryPartyAddress", + Address = "DeliveryPartyAddress,DeliveryPartyAddress,DeliveryPartyAddress", Name = "DeliveryPartyName" }, Vehicle = new VehicleInfo diff --git a/Excel.TemplateEngine/FileGenerating/Caches/CacheItems/AlignmentCacheItem.cs b/Excel.TemplateEngine/FileGenerating/Caches/CacheItems/AlignmentCacheItem.cs index 02b87ea..9d8b291 100644 --- a/Excel.TemplateEngine/FileGenerating/Caches/CacheItems/AlignmentCacheItem.cs +++ b/Excel.TemplateEngine/FileGenerating/Caches/CacheItems/AlignmentCacheItem.cs @@ -5,101 +5,81 @@ using SkbKontur.Excel.TemplateEngine.FileGenerating.DataTypes; -namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.CacheItems +namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.CacheItems; + +internal class AlignmentCacheItem : IEquatable { - internal class AlignmentCacheItem : IEquatable + public AlignmentCacheItem(ExcelCellAlignment cellAlignment) { - public AlignmentCacheItem(ExcelCellAlignment cellAlignment) - { - verticalAlignment = cellAlignment.VerticalAlignment; - horizontalAlignment = cellAlignment.HorizontalAlignment; - wrapText = cellAlignment.WrapText; - } + verticalAlignment = cellAlignment.VerticalAlignment; + horizontalAlignment = cellAlignment.HorizontalAlignment; + wrapText = cellAlignment.WrapText; + } - public bool Equals(AlignmentCacheItem other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return verticalAlignment == other.verticalAlignment && horizontalAlignment == other.horizontalAlignment && wrapText.Equals(other.wrapText); - } + public bool Equals(AlignmentCacheItem other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return verticalAlignment == other.verticalAlignment && horizontalAlignment == other.horizontalAlignment && wrapText.Equals(other.wrapText); + } - public Alignment ToAlignment() - { - return new Alignment - { - Horizontal = GetHorizontalAlignment(), - Vertical = GetVerticalAlignment(), - WrapText = wrapText ? new BooleanValue(true) : null - }; - } + public Alignment ToAlignment() + { + return new Alignment + { + Horizontal = GetHorizontalAlignment(), + Vertical = GetVerticalAlignment(), + WrapText = wrapText ? new BooleanValue(true) : null + }; + } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals((AlignmentCacheItem)obj); - } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((AlignmentCacheItem)obj); + } - public override int GetHashCode() + public override int GetHashCode() + { + unchecked { - unchecked - { - var hashCode = (int)verticalAlignment; - hashCode = (hashCode * 397) ^ (int)horizontalAlignment; - hashCode = (hashCode * 397) ^ wrapText.GetHashCode(); - return hashCode; - } + var hashCode = (int)verticalAlignment; + hashCode = (hashCode * 397) ^ (int)horizontalAlignment; + hashCode = (hashCode * 397) ^ wrapText.GetHashCode(); + return hashCode; } + } - private EnumValue GetVerticalAlignment() - { - EnumValue result; - switch (verticalAlignment) + private EnumValue GetVerticalAlignment() + { + EnumValue result = verticalAlignment switch { - case ExcelVerticalAlignment.Top: - result = new EnumValue(VerticalAlignmentValues.Top); - break; - case ExcelVerticalAlignment.Center: - result = new EnumValue(VerticalAlignmentValues.Center); - break; - case ExcelVerticalAlignment.Bottom: - result = new EnumValue(VerticalAlignmentValues.Bottom); - break; - case ExcelVerticalAlignment.Default: - result = null; - break; - default: - throw new InvalidOperationException($"Unknown vertical alignment: {verticalAlignment}"); - } - return result; - } + ExcelVerticalAlignment.Top => new EnumValue(VerticalAlignmentValues.Top), + ExcelVerticalAlignment.Center => new EnumValue(VerticalAlignmentValues.Center), + ExcelVerticalAlignment.Bottom => new EnumValue(VerticalAlignmentValues.Bottom), + ExcelVerticalAlignment.Default => null, + _ => throw new InvalidOperationException($"Unknown vertical alignment: {verticalAlignment}") + }; + return result; + } - private EnumValue GetHorizontalAlignment() - { - EnumValue result; - switch (horizontalAlignment) + private EnumValue GetHorizontalAlignment() + { + EnumValue result = horizontalAlignment switch { - case ExcelHorizontalAlignment.Left: - result = new EnumValue(HorizontalAlignmentValues.Left); - break; - case ExcelHorizontalAlignment.Center: - result = new EnumValue(HorizontalAlignmentValues.Center); - break; - case ExcelHorizontalAlignment.Right: - result = new EnumValue(HorizontalAlignmentValues.Right); - break; - case ExcelHorizontalAlignment.Default: - result = null; - break; - default: - throw new InvalidOperationException($"Unknown horizontal alignment: {horizontalAlignment}"); - } - return result; - } - - private readonly ExcelVerticalAlignment verticalAlignment; - private readonly ExcelHorizontalAlignment horizontalAlignment; - private readonly bool wrapText; + ExcelHorizontalAlignment.Left => new EnumValue(HorizontalAlignmentValues.Left), + ExcelHorizontalAlignment.Center => new EnumValue(HorizontalAlignmentValues.Center), + ExcelHorizontalAlignment.Right => new EnumValue(HorizontalAlignmentValues.Right), + ExcelHorizontalAlignment.Fill => new EnumValue(HorizontalAlignmentValues.Fill), + ExcelHorizontalAlignment.Default => null, + _ => throw new InvalidOperationException($"Unknown horizontal alignment: {horizontalAlignment}") + }; + return result; } + + private readonly ExcelVerticalAlignment verticalAlignment; + private readonly ExcelHorizontalAlignment horizontalAlignment; + private readonly bool wrapText; } \ No newline at end of file diff --git a/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs b/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs index b34c499..2f17571 100644 --- a/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs +++ b/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs @@ -17,257 +17,258 @@ using ColorType = DocumentFormat.OpenXml.Spreadsheet.ColorType; using Fill = DocumentFormat.OpenXml.Spreadsheet.Fill; -namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.Implementations +namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.Implementations; + +internal class ExcelDocumentStyle : IExcelDocumentStyle { - internal class ExcelDocumentStyle : IExcelDocumentStyle + public ExcelDocumentStyle(Stylesheet stylesheet, Theme theme, ILog logger) { - public ExcelDocumentStyle(Stylesheet stylesheet, Theme theme, ILog logger) - { - this.stylesheet = stylesheet; - this.logger = logger; - numberingFormats = new ExcelDocumentNumberingFormats(stylesheet); - fillStyles = new ExcelDocumentFillStyles(stylesheet); - bordersStyles = new ExcelDocumentBordersStyles(stylesheet); - fontStyles = new ExcelDocumentFontStyles(stylesheet); - // Not using theme.ThemeElements.ColorScheme.Elements() here because of wrong order. - colorSchemeElements = new List - { - theme.ThemeElements?.ColorScheme?.Light1Color, - theme.ThemeElements?.ColorScheme?.Dark1Color, - theme.ThemeElements?.ColorScheme?.Light2Color, - theme.ThemeElements?.ColorScheme?.Dark2Color, - theme.ThemeElements?.ColorScheme?.Accent1Color, - theme.ThemeElements?.ColorScheme?.Accent2Color, - theme.ThemeElements?.ColorScheme?.Accent3Color, - theme.ThemeElements?.ColorScheme?.Accent4Color, - theme.ThemeElements?.ColorScheme?.Accent5Color, - theme.ThemeElements?.ColorScheme?.Accent6Color, - theme.ThemeElements?.ColorScheme?.Hyperlink, - theme.ThemeElements?.ColorScheme?.FollowedHyperlinkColor, - }; - cache = new Dictionary(); - inverseCache = new Dictionary(); - } - - public uint AddStyle(ExcelCellStyle style) - { - var fillId = fillStyles.AddStyle(style.FillStyle); - var fontId = fontStyles.AddFont(style.FontStyle); - var borderId = bordersStyles.AddStyle(style.BordersStyle); - var numberFormatId = numberingFormats.AddFormat(style.NumberingFormat); - var alignment = Alignment(style.Alignment); - var cacheItem = new CellStyleCacheItem - { - FillId = fillId, - FontId = fontId, - BorderId = borderId, - NumberFormatId = numberFormatId, - Alignment = alignment - }; - if (!cache.TryGetValue(cacheItem, out var result) && stylesheet.CellFormats != null) + this.stylesheet = stylesheet; + this.logger = logger; + numberingFormats = new ExcelDocumentNumberingFormats(stylesheet); + fillStyles = new ExcelDocumentFillStyles(stylesheet); + bordersStyles = new ExcelDocumentBordersStyles(stylesheet); + fontStyles = new ExcelDocumentFontStyles(stylesheet); + // Not using theme.ThemeElements.ColorScheme.Elements() here because of wrong order. + colorSchemeElements = new List { - result = stylesheet.CellFormats.Count; - stylesheet.CellFormats.Count++; - stylesheet.CellFormats.AppendChild(cacheItem.ToCellFormat()); - cache.Add(cacheItem, result); - } - return result; - } + theme.ThemeElements?.ColorScheme?.Light1Color, + theme.ThemeElements?.ColorScheme?.Dark1Color, + theme.ThemeElements?.ColorScheme?.Light2Color, + theme.ThemeElements?.ColorScheme?.Dark2Color, + theme.ThemeElements?.ColorScheme?.Accent1Color, + theme.ThemeElements?.ColorScheme?.Accent2Color, + theme.ThemeElements?.ColorScheme?.Accent3Color, + theme.ThemeElements?.ColorScheme?.Accent4Color, + theme.ThemeElements?.ColorScheme?.Accent5Color, + theme.ThemeElements?.ColorScheme?.Accent6Color, + theme.ThemeElements?.ColorScheme?.Hyperlink, + theme.ThemeElements?.ColorScheme?.FollowedHyperlinkColor, + }; + cache = new Dictionary(); + inverseCache = new Dictionary(); + } - public ExcelCellStyle GetStyle(int styleIndex) + public uint AddStyle(ExcelCellStyle style) + { + var fillId = fillStyles.AddStyle(style.FillStyle); + var fontId = fontStyles.AddFont(style.FontStyle); + var borderId = bordersStyles.AddStyle(style.BordersStyle); + var numberFormatId = numberingFormats.AddFormat(style.NumberingFormat); + var alignment = Alignment(style.Alignment); + var cacheItem = new CellStyleCacheItem + { + FillId = fillId, + FontId = fontId, + BorderId = borderId, + NumberFormatId = numberFormatId, + Alignment = alignment + }; + if (!cache.TryGetValue(cacheItem, out var result) && stylesheet.CellFormats != null) { - if (inverseCache.TryGetValue((uint)styleIndex, out var result)) - return result; - - var cellFormat = stylesheet?.CellFormats?.ChildElements.Count > styleIndex ? (CellFormat)stylesheet.CellFormats.ChildElements[styleIndex] : null; - result = new ExcelCellStyle - { - FillStyle = cellFormat?.FillId == null ? null : GetCellFillStyle(cellFormat.FillId.Value), - FontStyle = cellFormat?.FontId == null ? null : GetCellFontStyle(cellFormat.FontId.Value), - NumberingFormat = cellFormat?.NumberFormatId == null ? null : GetCellNumberingFormat(cellFormat.NumberFormatId.Value), - BordersStyle = cellFormat?.BorderId == null ? null : GetCellBordersStyle(cellFormat.BorderId.Value), - Alignment = cellFormat?.Alignment == null ? null : GetCellAlignment(cellFormat.Alignment) - }; - inverseCache.Add((uint)styleIndex, result); + result = stylesheet.CellFormats.Count; + stylesheet.CellFormats.Count++; + stylesheet.CellFormats.AppendChild(cacheItem.ToCellFormat()); + cache.Add(cacheItem, result); + } + return result; + } + public ExcelCellStyle GetStyle(int styleIndex) + { + if (inverseCache.TryGetValue((uint)styleIndex, out var result)) return result; - } - private ExcelCellAlignment GetCellAlignment(Alignment alignment) - { - return new ExcelCellAlignment - { - WrapText = true, //alignment.With(a => a.WrapText) != null && alignment.WrapText.Value, всегда делать перенос по словам - HorizontalAlignment = alignment?.Horizontal == null ? ExcelHorizontalAlignment.Default : ToExcelHorizontalAlignment(alignment.Horizontal), - VerticalAlignment = alignment?.Vertical == null ? ExcelVerticalAlignment.Default : ToExcelVerticalAlignment(alignment.Vertical) - }; - } + var cellFormat = stylesheet?.CellFormats?.ChildElements.Count > styleIndex ? (CellFormat)stylesheet.CellFormats.ChildElements[styleIndex] : null; + result = new ExcelCellStyle + { + FillStyle = cellFormat?.FillId == null ? null : GetCellFillStyle(cellFormat.FillId.Value), + FontStyle = cellFormat?.FontId == null ? null : GetCellFontStyle(cellFormat.FontId.Value), + NumberingFormat = cellFormat?.NumberFormatId == null ? null : GetCellNumberingFormat(cellFormat.NumberFormatId.Value), + BordersStyle = cellFormat?.BorderId == null ? null : GetCellBordersStyle(cellFormat.BorderId.Value), + Alignment = cellFormat?.Alignment == null ? null : GetCellAlignment(cellFormat.Alignment) + }; + inverseCache.Add((uint)styleIndex, result); - private ExcelVerticalAlignment ToExcelVerticalAlignment(EnumValue vertical) - { - if (vertical.Value == VerticalAlignmentValues.Bottom) - return ExcelVerticalAlignment.Bottom; - if (vertical.Value == VerticalAlignmentValues.Center) - return ExcelVerticalAlignment.Center; - if (vertical.Value == VerticalAlignmentValues.Top) - return ExcelVerticalAlignment.Top; - return ExcelVerticalAlignment.Default; - } + return result; + } - private ExcelHorizontalAlignment ToExcelHorizontalAlignment(EnumValue horizontal) - { - if (horizontal.Value == HorizontalAlignmentValues.Center) - return ExcelHorizontalAlignment.Center; - if (horizontal.Value == HorizontalAlignmentValues.Left) - return ExcelHorizontalAlignment.Left; - if (horizontal.Value == HorizontalAlignmentValues.Right) - return ExcelHorizontalAlignment.Right; - return ExcelHorizontalAlignment.Default; - } + private ExcelCellAlignment GetCellAlignment(Alignment alignment) + { + return new ExcelCellAlignment + { + WrapText = true, //alignment.With(a => a.WrapText) != null && alignment.WrapText.Value, всегда делать перенос по словам + HorizontalAlignment = alignment?.Horizontal == null ? ExcelHorizontalAlignment.Default : ToExcelHorizontalAlignment(alignment.Horizontal), + VerticalAlignment = alignment?.Vertical == null ? ExcelVerticalAlignment.Default : ToExcelVerticalAlignment(alignment.Vertical) + }; + } - private ExcelCellBordersStyle GetCellBordersStyle(uint borderId) - { - var bordersStyle = stylesheet?.Borders?.ChildElements.Count > borderId ? (Border)stylesheet.Borders.ChildElements[(int)borderId] : null; - return new ExcelCellBordersStyle - { - LeftBorder = bordersStyle?.LeftBorder == null ? null : GetBorderStyle(bordersStyle.LeftBorder), - RightBorder = bordersStyle?.RightBorder == null ? null : GetBorderStyle(bordersStyle.RightBorder), - TopBorder = bordersStyle?.TopBorder == null ? null : GetBorderStyle(bordersStyle.TopBorder), - BottomBorder = bordersStyle?.BottomBorder == null ? null : GetBorderStyle(bordersStyle.BottomBorder) - }; - } + private ExcelVerticalAlignment ToExcelVerticalAlignment(EnumValue vertical) + { + if (vertical.Value == VerticalAlignmentValues.Bottom) + return ExcelVerticalAlignment.Bottom; + if (vertical.Value == VerticalAlignmentValues.Center) + return ExcelVerticalAlignment.Center; + if (vertical.Value == VerticalAlignmentValues.Top) + return ExcelVerticalAlignment.Top; + return ExcelVerticalAlignment.Default; + } - private ExcelCellBorderStyle GetBorderStyle(BorderPropertiesType border) - { - return new ExcelCellBorderStyle - { - Color = ToExcelColor(border?.Color), - BorderType = border?.Style == null ? ExcelBorderType.None : ToExcelBorderType(border.Style) - }; - } + private ExcelHorizontalAlignment ToExcelHorizontalAlignment(EnumValue horizontal) + { + if (horizontal.Value == HorizontalAlignmentValues.Center) + return ExcelHorizontalAlignment.Center; + if (horizontal.Value == HorizontalAlignmentValues.Left) + return ExcelHorizontalAlignment.Left; + if (horizontal.Value == HorizontalAlignmentValues.Right) + return ExcelHorizontalAlignment.Right; + if (horizontal.Value == HorizontalAlignmentValues.Fill) + return ExcelHorizontalAlignment.Fill; + return ExcelHorizontalAlignment.Default; + } - private static ExcelBorderType ToExcelBorderType(EnumValue borderStyle) - { - if (borderStyle.Value == BorderStyleValues.None) - return ExcelBorderType.None; - if (borderStyle.Value == BorderStyleValues.Thin) - return ExcelBorderType.Thin; - if (borderStyle.Value == BorderStyleValues.Medium) - return ExcelBorderType.Single; - if (borderStyle.Value == BorderStyleValues.Thick) - return ExcelBorderType.Bold; - if (borderStyle.Value == BorderStyleValues.Double) - return ExcelBorderType.Double; - throw new InvalidOperationException($"Unknown border type: {borderStyle}"); - } + private ExcelCellBordersStyle GetCellBordersStyle(uint borderId) + { + var bordersStyle = stylesheet?.Borders?.ChildElements.Count > borderId ? (Border)stylesheet.Borders.ChildElements[(int)borderId] : null; + return new ExcelCellBordersStyle + { + LeftBorder = bordersStyle?.LeftBorder == null ? null : GetBorderStyle(bordersStyle.LeftBorder), + RightBorder = bordersStyle?.RightBorder == null ? null : GetBorderStyle(bordersStyle.RightBorder), + TopBorder = bordersStyle?.TopBorder == null ? null : GetBorderStyle(bordersStyle.TopBorder), + BottomBorder = bordersStyle?.BottomBorder == null ? null : GetBorderStyle(bordersStyle.BottomBorder) + }; + } - private ExcelCellNumberingFormat GetCellNumberingFormat(uint numberFormatId) - { - if (standardNumberingFormatsId.Contains(numberFormatId)) - return new ExcelCellNumberingFormat(numberFormatId); + private ExcelCellBorderStyle GetBorderStyle(BorderPropertiesType border) + { + return new ExcelCellBorderStyle + { + Color = ToExcelColor(border?.Color), + BorderType = border?.Style == null ? ExcelBorderType.None : ToExcelBorderType(border.Style) + }; + } - var numberFormat = (NumberingFormat)stylesheet?.NumberingFormats?.ChildElements - .FirstOrDefault(ce => ((NumberingFormat)ce)?.NumberFormatId != null && - ((NumberingFormat)ce).NumberFormatId!.Value == numberFormatId); - if (numberFormat?.FormatCode?.Value == null) - return null; + private static ExcelBorderType ToExcelBorderType(EnumValue borderStyle) + { + if (borderStyle.Value == BorderStyleValues.None) + return ExcelBorderType.None; + if (borderStyle.Value == BorderStyleValues.Thin) + return ExcelBorderType.Thin; + if (borderStyle.Value == BorderStyleValues.Medium) + return ExcelBorderType.Single; + if (borderStyle.Value == BorderStyleValues.Thick) + return ExcelBorderType.Bold; + if (borderStyle.Value == BorderStyleValues.Double) + return ExcelBorderType.Double; + throw new InvalidOperationException($"Unknown border type: {borderStyle}"); + } - return new ExcelCellNumberingFormat(numberFormat.NumberFormatId!.Value, numberFormat.FormatCode.Value); - } + private ExcelCellNumberingFormat GetCellNumberingFormat(uint numberFormatId) + { + if (standardNumberingFormatsId.Contains(numberFormatId)) + return new ExcelCellNumberingFormat(numberFormatId); - private ExcelCellFontStyle GetCellFontStyle(uint fontId) - { - var internalFont = stylesheet?.Fonts?.ChildElements.Count > fontId ? (Font)stylesheet.Fonts.ChildElements[(int)fontId] : null; - return new ExcelCellFontStyle - { - Bold = internalFont?.Bold != null, - Size = internalFont?.FontSize == null ? (int?)null : Convert.ToInt32((object)internalFont.FontSize?.Val?.Value), - Underlined = internalFont?.Underline != null, - Color = ToExcelColor(internalFont?.Color) - }; - } + var numberFormat = (NumberingFormat)stylesheet?.NumberingFormats?.ChildElements + .FirstOrDefault(ce => ((NumberingFormat)ce)?.NumberFormatId != null && + ((NumberingFormat)ce).NumberFormatId!.Value == numberFormatId); + if (numberFormat?.FormatCode?.Value == null) + return null; - private ExcelCellFillStyle GetCellFillStyle(uint fillId) - { - var fill = stylesheet?.Fills?.ChildElements.Count > fillId ? (Fill)stylesheet.Fills.ChildElements[(int)fillId] : null; - var color = ToExcelColor(fill?.PatternFill?.ForegroundColor); + return new ExcelCellNumberingFormat(numberFormat.NumberFormatId!.Value, numberFormat.FormatCode.Value); + } - if (color == null) - return null; + private ExcelCellFontStyle GetCellFontStyle(uint fontId) + { + var internalFont = stylesheet?.Fonts?.ChildElements.Count > fontId ? (Font)stylesheet.Fonts.ChildElements[(int)fontId] : null; + return new ExcelCellFontStyle + { + Bold = internalFont?.Bold != null, + Size = internalFont?.FontSize == null ? (int?)null : Convert.ToInt32((object)internalFont.FontSize?.Val?.Value), + Underlined = internalFont?.Underline != null, + Color = ToExcelColor(internalFont?.Color) + }; + } - return new ExcelCellFillStyle {Color = color}; - } + private ExcelCellFillStyle GetCellFillStyle(uint fillId) + { + var fill = stylesheet?.Fills?.ChildElements.Count > fillId ? (Fill)stylesheet.Fills.ChildElements[(int)fillId] : null; + var color = ToExcelColor(fill?.PatternFill?.ForegroundColor); - [CanBeNull] - private ExcelColor ToExcelColor([CanBeNull] ColorType color) - { - if (color == null) - return null; - if (color.Rgb?.HasValue == true) - { - return RgbStringToExcelColor(color.Rgb.Value!); - } - if (color.Theme?.HasValue == true) - { - var theme = color.Theme.Value; - var tint = color.Tint?.Value ?? 0; - return ThemeToExcelColor(theme, tint); - } + if (color == null) return null; - } - [CanBeNull] - private static ExcelColor RgbStringToExcelColor([NotNull] string hexRgbColor) + return new ExcelCellFillStyle {Color = color}; + } + + [CanBeNull] + private ExcelColor ToExcelColor([CanBeNull] ColorType color) + { + if (color == null) + return null; + if (color.Rgb?.HasValue == true) { - if (hexRgbColor.Length == 6) - hexRgbColor = "FF" + hexRgbColor; - return new ExcelColor(alpha : Convert.ToInt32(hexRgbColor.Substring(0, 2), 16), - red : Convert.ToInt32(hexRgbColor.Substring(2, 2), 16), - green : Convert.ToInt32(hexRgbColor.Substring(4, 2), 16), - blue : Convert.ToInt32(hexRgbColor.Substring(6, 2), 16)); + return RgbStringToExcelColor(color.Rgb.Value!); } - - [CanBeNull] - private ExcelColor ThemeToExcelColor(uint theme, double tint) + if (color.Theme?.HasValue == true) { - if (theme >= colorSchemeElements.Count) - throw new InvalidOperationException($"Theme with id '{theme}' not found"); - var color2Type = colorSchemeElements[(int)theme]; - var rgbColor = color2Type?.RgbColorModelHex?.Val?.Value ?? color2Type?.SystemColor?.LastColor?.Value; - if (rgbColor == null) - { - logger.Error("Failed to get rgbColor from theme"); - return null; - } - var hls = ColorConverter.RgbToHls(RgbStringToExcelColor(rgbColor)); - if (tint < 0) - hls.L = hls.L * (1.0 + tint); - else - hls.L = hls.L * (1.0 - tint) + tint; - return ColorConverter.HslToRgb(hls); + var theme = color.Theme.Value; + var tint = color.Tint?.Value ?? 0; + return ThemeToExcelColor(theme, tint); } + return null; + } + + [CanBeNull] + private static ExcelColor RgbStringToExcelColor([NotNull] string hexRgbColor) + { + if (hexRgbColor.Length == 6) + hexRgbColor = "FF" + hexRgbColor; + return new ExcelColor(alpha : Convert.ToInt32(hexRgbColor.Substring(0, 2), 16), + red : Convert.ToInt32(hexRgbColor.Substring(2, 2), 16), + green : Convert.ToInt32(hexRgbColor.Substring(4, 2), 16), + blue : Convert.ToInt32(hexRgbColor.Substring(6, 2), 16)); + } - private static AlignmentCacheItem Alignment(ExcelCellAlignment cellAlignment) + [CanBeNull] + private ExcelColor ThemeToExcelColor(uint theme, double tint) + { + if (theme >= colorSchemeElements.Count) + throw new InvalidOperationException($"Theme with id '{theme}' not found"); + var color2Type = colorSchemeElements[(int)theme]; + var rgbColor = color2Type?.RgbColorModelHex?.Val?.Value ?? color2Type?.SystemColor?.LastColor?.Value; + if (rgbColor == null) { - if (cellAlignment == null) - return null; - if (cellAlignment.HorizontalAlignment == ExcelHorizontalAlignment.Default && cellAlignment.VerticalAlignment == ExcelVerticalAlignment.Default && !cellAlignment.WrapText) - return null; - return new AlignmentCacheItem(cellAlignment); + logger.Error("Failed to get rgbColor from theme"); + return null; } + var hls = ColorConverter.RgbToHls(RgbStringToExcelColor(rgbColor)); + if (tint < 0) + hls.L = hls.L * (1.0 + tint); + else + hls.L = hls.L * (1.0 - tint) + tint; + return ColorConverter.HslToRgb(hls); + } - // standardNumberingFormatsId -- set of numbering formats that can be identified without format codes - // https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-2.8.1 - private static HashSet standardNumberingFormatsId = new HashSet {0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 45, 46, 47, 48, 49}; - - private readonly Stylesheet stylesheet; - private readonly ILog logger; - private readonly ExcelDocumentNumberingFormats numberingFormats; - private readonly ExcelDocumentFillStyles fillStyles; - private readonly ExcelDocumentBordersStyles bordersStyles; - private readonly IExcelDocumentFontStyles fontStyles; - private readonly List colorSchemeElements; - private readonly IDictionary cache; - private readonly IDictionary inverseCache; + private static AlignmentCacheItem Alignment(ExcelCellAlignment cellAlignment) + { + if (cellAlignment == null) + return null; + if (cellAlignment.HorizontalAlignment == ExcelHorizontalAlignment.Default && cellAlignment.VerticalAlignment == ExcelVerticalAlignment.Default && !cellAlignment.WrapText) + return null; + return new AlignmentCacheItem(cellAlignment); } + + // standardNumberingFormatsId -- set of numbering formats that can be identified without format codes + // https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.numberingformat?view=openxml-2.8.1 + private static HashSet standardNumberingFormatsId = new HashSet {0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 45, 46, 47, 48, 49}; + + private readonly Stylesheet stylesheet; + private readonly ILog logger; + private readonly ExcelDocumentNumberingFormats numberingFormats; + private readonly ExcelDocumentFillStyles fillStyles; + private readonly ExcelDocumentBordersStyles bordersStyles; + private readonly IExcelDocumentFontStyles fontStyles; + private readonly List colorSchemeElements; + private readonly IDictionary cache; + private readonly IDictionary inverseCache; } \ No newline at end of file diff --git a/Excel.TemplateEngine/FileGenerating/DataTypes/ExcelHorizontalAlignment.cs b/Excel.TemplateEngine/FileGenerating/DataTypes/ExcelHorizontalAlignment.cs index abb230b..8da453d 100644 --- a/Excel.TemplateEngine/FileGenerating/DataTypes/ExcelHorizontalAlignment.cs +++ b/Excel.TemplateEngine/FileGenerating/DataTypes/ExcelHorizontalAlignment.cs @@ -1,10 +1,10 @@ -namespace SkbKontur.Excel.TemplateEngine.FileGenerating.DataTypes +namespace SkbKontur.Excel.TemplateEngine.FileGenerating.DataTypes; + +public enum ExcelHorizontalAlignment { - public enum ExcelHorizontalAlignment - { - Default, - Left, - Center, - Right - } + Default, + Left, + Center, + Right, + Fill, } \ No newline at end of file