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
36internal 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 ) ;
0 commit comments