diff --git a/src/MiniExcel.Core/Enums/CellAlignment.cs b/src/MiniExcel.Core/Enums/CellAlignment.cs
new file mode 100644
index 00000000..78ca724b
--- /dev/null
+++ b/src/MiniExcel.Core/Enums/CellAlignment.cs
@@ -0,0 +1,15 @@
+namespace MiniExcelLib.Core.Enums;
+
+public enum HorizontalCellAlignment
+{
+ Left,
+ Center,
+ Right
+}
+
+public enum VerticalCellAlignment
+{
+ Top,
+ Center,
+ Bottom
+}
\ No newline at end of file
diff --git a/src/MiniExcel.Core/OpenXml/Styles/Builder/DefaultSheetStyleBuilder.cs b/src/MiniExcel.Core/OpenXml/Styles/Builder/DefaultSheetStyleBuilder.cs
index 3f4d387e..56a4b097 100644
--- a/src/MiniExcel.Core/OpenXml/Styles/Builder/DefaultSheetStyleBuilder.cs
+++ b/src/MiniExcel.Core/OpenXml/Styles/Builder/DefaultSheetStyleBuilder.cs
@@ -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)
@@ -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;
@@ -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);
@@ -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);
@@ -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);
diff --git a/src/MiniExcel.Core/OpenXml/Styles/OpenXmlHeaderStyle.cs b/src/MiniExcel.Core/OpenXml/Styles/OpenXmlHeaderStyle.cs
new file mode 100644
index 00000000..ef75a92e
--- /dev/null
+++ b/src/MiniExcel.Core/OpenXml/Styles/OpenXmlHeaderStyle.cs
@@ -0,0 +1,27 @@
+using System.Drawing;
+using MiniExcelLib.Core.Enums;
+
+namespace MiniExcelLib.Core.OpenXml.Styles;
+
+public class OpenXmlHeaderStyle
+{
+ ///
+ /// Whether to wrap the content of the header
+ ///
+ public bool WrapText { get; set; }
+
+ ///
+ /// The RGB background color in the filtered state
+ ///
+ public Color BackgroundColor { get; set; } = Color.FromArgb(0x284472C4);
+
+ ///
+ /// Horizontal alignment
+ ///
+ public HorizontalCellAlignment HorizontalAlignment { get; set; } = HorizontalCellAlignment.Left;
+
+ ///
+ /// Vertical alignment
+ ///
+ public VerticalCellAlignment VerticalAlignment { get; set; } = VerticalCellAlignment.Bottom;
+}
\ No newline at end of file
diff --git a/src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs b/src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs
index 68261879..06afe430 100644
--- a/src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs
+++ b/src/MiniExcel.Core/OpenXml/Styles/OpenXmlStyleOptions.cs
@@ -3,4 +3,5 @@ namespace MiniExcelLib.Core.OpenXml.Styles;
public class OpenXmlStyleOptions
{
public bool WrapCellContents { get; set; }
+ public OpenXmlHeaderStyle? HeaderStyle { get; set; }
}
\ No newline at end of file