Skip to content

Commit 74ff1af

Browse files
dazhu6666michelebastioneshps951023
authored
Adding header styling options as part of the OpenXmlStyleOptions class (#841)
Co-authored-by: Michele Bastione <michele.bastione@gmail.com> Co-authored-by: Wei Lin <shps951002@gmail.com>
1 parent 4d697fa commit 74ff1af

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MiniExcelLib.Core.Enums;
2+
3+
public enum HorizontalCellAlignment
4+
{
5+
Left,
6+
Center,
7+
Right
8+
}
9+
10+
public enum VerticalCellAlignment
11+
{
12+
Top,
13+
Center,
14+
Bottom
15+
}

src/MiniExcel.Core/OpenXml/Styles/Builder/DefaultSheetStyleBuilder.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace MiniExcelLib.Core.OpenXml.Styles.Builder;
1+
using System.Drawing;
2+
using MiniExcelLib.Core.Enums;
3+
4+
namespace MiniExcelLib.Core.OpenXml.Styles.Builder;
25

36
internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context, OpenXmlStyleOptions styleOptions)
47
: SheetStyleBuilderBase(context)
@@ -13,6 +16,10 @@ internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context,
1316
CellXfCount = 5
1417
};
1518

19+
private static readonly Color DefaultBackgroundColor = Color.FromArgb(0x284472C4);
20+
private const HorizontalCellAlignment DefaultHorizontalAlignment = HorizontalCellAlignment.Left;
21+
private const VerticalCellAlignment DefaultVerticalAlignment = VerticalCellAlignment.Bottom;
22+
1623
private readonly SheetStyleBuildContext _context = context;
1724
private readonly OpenXmlStyleOptions _styleOptions = styleOptions;
1825

@@ -134,7 +141,11 @@ protected override async Task GenerateFillAsync()
134141
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "patternFill", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
135142
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "patternType", null, "solid").ConfigureAwait(false);
136143
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "fgColor", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
137-
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "rgb", null, "284472C4").ConfigureAwait(false);
144+
145+
var bgColor = _styleOptions.HeaderStyle?.BackgroundColor ?? DefaultBackgroundColor;
146+
var hexBgColor = $"{bgColor.A:X2}{bgColor.R:X2}{bgColor.G:X2}{bgColor.B:X2}";
147+
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "rgb", null, hexBgColor).ConfigureAwait(false);
148+
138149
await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
139150
await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
140151
await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
@@ -346,10 +357,20 @@ protected override async Task GenerateCellXfAsync()
346357
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
347358
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
348359
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "alignment", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
349-
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, "left").ConfigureAwait(false);
350-
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
360+
361+
var horizontalAlignment = _styleOptions.HeaderStyle?.HorizontalAlignment ?? DefaultHorizontalAlignment;
362+
var horizontalAlignmentStr = horizontalAlignment.ToString().ToLowerInvariant();
363+
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, horizontalAlignmentStr).ConfigureAwait(false);
364+
365+
var verticalAlignment = _styleOptions.HeaderStyle?.VerticalAlignment ?? DefaultVerticalAlignment;
366+
var verticalAlignmentStr = verticalAlignment.ToString().ToLowerInvariant();
367+
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, verticalAlignmentStr).ConfigureAwait(false);
368+
351369
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
352-
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, "0").ConfigureAwait(false);
370+
371+
var wrapHeader = (_styleOptions.HeaderStyle?.WrapText ?? false) ? "1" : "0";
372+
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapHeader).ConfigureAwait(false);
373+
353374
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
354375
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
355376
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
@@ -383,7 +404,10 @@ protected override async Task GenerateCellXfAsync()
383404
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, "general").ConfigureAwait(false);
384405
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
385406
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
386-
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, _styleOptions.WrapCellContents ? "1" : "0").ConfigureAwait(false);
407+
408+
var wrapContent = _styleOptions.WrapCellContents ? "1" : "0";
409+
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapContent).ConfigureAwait(false);
410+
387411
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
388412
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
389413
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Drawing;
2+
using MiniExcelLib.Core.Enums;
3+
4+
namespace MiniExcelLib.Core.OpenXml.Styles;
5+
6+
public class OpenXmlHeaderStyle
7+
{
8+
/// <summary>
9+
/// Whether to wrap the content of the header
10+
/// </summary>
11+
public bool WrapText { get; set; }
12+
13+
/// <summary>
14+
/// The RGB background color in the filtered state
15+
/// </summary>
16+
public Color BackgroundColor { get; set; } = Color.FromArgb(0x284472C4);
17+
18+
/// <summary>
19+
/// Horizontal alignment
20+
/// </summary>
21+
public HorizontalCellAlignment HorizontalAlignment { get; set; } = HorizontalCellAlignment.Left;
22+
23+
/// <summary>
24+
/// Vertical alignment
25+
/// </summary>
26+
public VerticalCellAlignment VerticalAlignment { get; set; } = VerticalCellAlignment.Bottom;
27+
}

src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ namespace MiniExcelLib.Core.OpenXml.Styles;
33
public class OpenXmlStyleOptions
44
{
55
public bool WrapCellContents { get; set; }
6+
public OpenXmlHeaderStyle? HeaderStyle { get; set; }
67
}

0 commit comments

Comments
 (0)