1
- namespace MiniExcelLib . Core . OpenXml . Styles . Builder ;
1
+ using System . Drawing ;
2
+ using MiniExcelLib . Core . Enums ;
3
+
4
+ namespace MiniExcelLib . Core . OpenXml . Styles . Builder ;
2
5
3
6
internal partial class DefaultSheetStyleBuilder ( SheetStyleBuildContext context , OpenXmlStyleOptions styleOptions )
4
7
: SheetStyleBuilderBase ( context )
@@ -13,6 +16,10 @@ internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context,
13
16
CellXfCount = 5
14
17
} ;
15
18
19
+ private static readonly Color DefaultBackgroundColor = Color . FromArgb ( 0x284472C4 ) ;
20
+ private const HorizontalCellAlignment DefaultHorizontalAlignment = HorizontalCellAlignment . Left ;
21
+ private const VerticalCellAlignment DefaultVerticalAlignment = VerticalCellAlignment . Bottom ;
22
+
16
23
private readonly SheetStyleBuildContext _context = context ;
17
24
private readonly OpenXmlStyleOptions _styleOptions = styleOptions ;
18
25
@@ -134,7 +141,11 @@ protected override async Task GenerateFillAsync()
134
141
await _context . NewXmlWriter . WriteStartElementAsync ( _context . OldXmlReader . Prefix , "patternFill" , _context . OldXmlReader . NamespaceURI ) . ConfigureAwait ( false ) ;
135
142
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "patternType" , null , "solid" ) . ConfigureAwait ( false ) ;
136
143
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
+
138
149
await _context . NewXmlWriter . WriteEndElementAsync ( ) . ConfigureAwait ( false ) ;
139
150
await _context . NewXmlWriter . WriteEndElementAsync ( ) . ConfigureAwait ( false ) ;
140
151
await _context . NewXmlWriter . WriteEndElementAsync ( ) . ConfigureAwait ( false ) ;
@@ -346,10 +357,20 @@ protected override async Task GenerateCellXfAsync()
346
357
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "applyAlignment" , null , "1" ) . ConfigureAwait ( false ) ;
347
358
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "applyProtection" , null , "1" ) . ConfigureAwait ( false ) ;
348
359
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
+
351
369
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
+
353
374
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "indent" , null , "0" ) . ConfigureAwait ( false ) ;
354
375
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "relativeIndent" , null , "0" ) . ConfigureAwait ( false ) ;
355
376
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "justifyLastLine" , null , "0" ) . ConfigureAwait ( false ) ;
@@ -383,7 +404,10 @@ protected override async Task GenerateCellXfAsync()
383
404
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "horizontal" , null , "general" ) . ConfigureAwait ( false ) ;
384
405
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "vertical" , null , "bottom" ) . ConfigureAwait ( false ) ;
385
406
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
+
387
411
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "indent" , null , "0" ) . ConfigureAwait ( false ) ;
388
412
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "relativeIndent" , null , "0" ) . ConfigureAwait ( false ) ;
389
413
await _context . NewXmlWriter . WriteAttributeStringAsync ( null , "justifyLastLine" , null , "0" ) . ConfigureAwait ( false ) ;
0 commit comments