Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/MiniExcel.Core/Enums/CellAlignment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MiniExcelLib.Core.Enums;

public enum HorizontalCellAlignment
{
Left,
Center,
Right
}

public enum VerticalCellAlignment
{
Top,
Center,
Bottom
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace MiniExcelLib.Core.OpenXml.Styles.Builder;
using System.Drawing;
using MiniExcelLib.Core.Enums;

namespace MiniExcelLib.Core.OpenXml.Styles.Builder;

internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context, OpenXmlStyleOptions styleOptions)
: SheetStyleBuilderBase(context)
Expand All @@ -13,6 +16,10 @@ internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context,
CellXfCount = 5
};

private static readonly Color DefaultBackgroundColor = Color.FromArgb(0x284472C4);
private const HorizontalCellAlignment DefaultHorizontalAlignment = HorizontalCellAlignment.Left;
private const VerticalCellAlignment DefaultVerticalAlignment = VerticalCellAlignment.Bottom;

private readonly SheetStyleBuildContext _context = context;
private readonly OpenXmlStyleOptions _styleOptions = styleOptions;

Expand Down Expand Up @@ -134,7 +141,11 @@ protected override async Task GenerateFillAsync()
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "patternFill", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "patternType", null, "solid").ConfigureAwait(false);
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "fgColor", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "rgb", null, "284472C4").ConfigureAwait(false);

var bgColor = _styleOptions.HeaderStyle?.BackgroundColor ?? DefaultBackgroundColor;
var hexBgColor = $"{bgColor.A:X2}{bgColor.R:X2}{bgColor.G:X2}{bgColor.B:X2}";
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "rgb", null, hexBgColor).ConfigureAwait(false);

await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
await _context.NewXmlWriter.WriteEndElementAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -346,10 +357,20 @@ protected override async Task GenerateCellXfAsync()
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
await _context.NewXmlWriter.WriteStartElementAsync(_context.OldXmlReader.Prefix, "alignment", _context.OldXmlReader.NamespaceURI).ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, "left").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);

var horizontalAlignment = _styleOptions.HeaderStyle?.HorizontalAlignment ?? DefaultHorizontalAlignment;
var horizontalAlignmentStr = horizontalAlignment.ToString().ToLowerInvariant();
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, horizontalAlignmentStr).ConfigureAwait(false);

var verticalAlignment = _styleOptions.HeaderStyle?.VerticalAlignment ?? DefaultVerticalAlignment;
var verticalAlignmentStr = verticalAlignment.ToString().ToLowerInvariant();
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, verticalAlignmentStr).ConfigureAwait(false);

await _context.NewXmlWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, "0").ConfigureAwait(false);

var wrapHeader = (_styleOptions.HeaderStyle?.WrapText ?? false) ? "1" : "0";
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapHeader).ConfigureAwait(false);

await _context.NewXmlWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
Expand Down Expand Up @@ -383,7 +404,10 @@ protected override async Task GenerateCellXfAsync()
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "horizontal", null, "general").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, _styleOptions.WrapCellContents ? "1" : "0").ConfigureAwait(false);

var wrapContent = _styleOptions.WrapCellContents ? "1" : "0";
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapContent).ConfigureAwait(false);

await _context.NewXmlWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
await _context.NewXmlWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
Expand Down
27 changes: 27 additions & 0 deletions src/MiniExcel.Core/OpenXml/Styles/OpenXmlHeaderStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Drawing;
using MiniExcelLib.Core.Enums;

namespace MiniExcelLib.Core.OpenXml.Styles;

public class OpenXmlHeaderStyle
{
/// <summary>
/// Whether to wrap the content of the header
/// </summary>
public bool WrapText { get; set; }

/// <summary>
/// The RGB background color in the filtered state
/// </summary>
public Color BackgroundColor { get; set; } = Color.FromArgb(0x284472C4);

/// <summary>
/// Horizontal alignment
/// </summary>
public HorizontalCellAlignment HorizontalAlignment { get; set; } = HorizontalCellAlignment.Left;

/// <summary>
/// Vertical alignment
/// </summary>
public VerticalCellAlignment VerticalAlignment { get; set; } = VerticalCellAlignment.Bottom;
}
1 change: 1 addition & 0 deletions src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ namespace MiniExcelLib.Core.OpenXml.Styles;
public class OpenXmlStyleOptions
{
public bool WrapCellContents { get; set; }
public OpenXmlHeaderStyle? HeaderStyle { get; set; }
}
Loading