Skip to content

Commit b2ecec2

Browse files
Cell alignment support (#938)
Added horizontal and vertical cell alignment support for all rows besides the header --------- Co-authored-by: Michele Bastione <michele.bastione@gmail.com>
1 parent a0a1285 commit b2ecec2

3 files changed

Lines changed: 82 additions & 37 deletions

File tree

src/Directory.Packages.props

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<Project>
2-
3-
<ItemGroup>
4-
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.6.13" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive"/>
5-
</ItemGroup>
6-
7-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
8-
<PackageReference Include="Microsoft.Bcl.Memory" Version="10.0.0"/>
9-
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.0"/>
10-
</ItemGroup>
11-
12-
<ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
13-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
14-
</ItemGroup>
15-
2+
<ItemGroup>
3+
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.6.13" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
4+
</ItemGroup>
5+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
6+
<PackageReference Include="Microsoft.Bcl.Memory" Version="10.0.5" />
7+
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.0" />
8+
</ItemGroup>
9+
<ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
10+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
11+
</ItemGroup>
1612
</Project>

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

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ internal partial class DefaultSheetStyleBuilder(SheetStyleBuildContext context,
1919
private static readonly Color DefaultBackgroundColor = Color.FromArgb(0x284472C4);
2020
private const HorizontalCellAlignment DefaultHorizontalAlignment = HorizontalCellAlignment.Left;
2121
private const VerticalCellAlignment DefaultVerticalAlignment = VerticalCellAlignment.Bottom;
22-
22+
2323
private readonly SheetStyleBuildContext _context = context;
2424
private readonly OpenXmlStyleOptions _styleOptions = styleOptions;
2525

2626
private XmlReader OldReader => _context.OldXmlReader!;
27-
private XmlWriter NewWriter => _context.NewXmlWriter!;
27+
private XmlWriter NewWriter => _context.NewXmlWriter!;
2828

2929
protected override SheetStyleElementInfos GetGenerateElementInfos()
3030
{
@@ -148,7 +148,7 @@ protected override async Task GenerateFillAsync()
148148
var bgColor = _styleOptions.HeaderStyle?.BackgroundColor ?? DefaultBackgroundColor;
149149
var hexBgColor = $"{bgColor.A:X2}{bgColor.R:X2}{bgColor.G:X2}{bgColor.B:X2}";
150150
await NewWriter.WriteAttributeStringAsync(null, "rgb", null, hexBgColor).ConfigureAwait(false);
151-
151+
152152
await NewWriter.WriteEndElementAsync().ConfigureAwait(false);
153153
await NewWriter.WriteEndElementAsync().ConfigureAwait(false);
154154
await NewWriter.WriteEndElementAsync().ConfigureAwait(false);
@@ -360,20 +360,20 @@ protected override async Task GenerateCellXfAsync()
360360
await NewWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
361361
await NewWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
362362
await NewWriter.WriteStartElementAsync(OldReader.Prefix, "alignment", OldReader.NamespaceURI).ConfigureAwait(false);
363-
364-
var horizontalAlignment = _styleOptions.HeaderStyle?.HorizontalAlignment ?? DefaultHorizontalAlignment;
365-
var horizontalAlignmentStr = horizontalAlignment.ToString().ToLowerInvariant();
366-
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, horizontalAlignmentStr).ConfigureAwait(false);
367-
368-
var verticalAlignment = _styleOptions.HeaderStyle?.VerticalAlignment ?? DefaultVerticalAlignment;
369-
var verticalAlignmentStr = verticalAlignment.ToString().ToLowerInvariant();
370-
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, verticalAlignmentStr).ConfigureAwait(false);
371-
363+
364+
var headerHorizontalAlignment = _styleOptions.HeaderStyle?.HorizontalAlignment ?? DefaultHorizontalAlignment;
365+
var headerHorizontalAlignmentStr = headerHorizontalAlignment.ToString().ToLowerInvariant();
366+
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, headerHorizontalAlignmentStr).ConfigureAwait(false);
367+
368+
var headerVerticalAlignment = _styleOptions.HeaderStyle?.VerticalAlignment ?? DefaultVerticalAlignment;
369+
var headerVerticalAlignmentStr = headerVerticalAlignment.ToString().ToLowerInvariant();
370+
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, headerVerticalAlignmentStr).ConfigureAwait(false);
371+
372372
await NewWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
373373

374374
var wrapHeader = (_styleOptions.HeaderStyle?.WrapText ?? false) ? "1" : "0";
375375
await NewWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapHeader).ConfigureAwait(false);
376-
376+
377377
await NewWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
378378
await NewWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
379379
await NewWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
@@ -404,13 +404,28 @@ protected override async Task GenerateCellXfAsync()
404404
await NewWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
405405
await NewWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
406406
await NewWriter.WriteStartElementAsync(OldReader.Prefix, "alignment", OldReader.NamespaceURI).ConfigureAwait(false);
407-
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, "general").ConfigureAwait(false);
408-
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
409-
await NewWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
407+
408+
var style1HorizontalAlignment = _styleOptions.HorizontalAlignment switch
409+
{
410+
HorizontalCellAlignment.Center => "center",
411+
HorizontalCellAlignment.Right => "right",
412+
_ => "general"
413+
};
414+
415+
var style1VerticalAlignment = _styleOptions.VerticalAlignment switch
416+
{
417+
VerticalCellAlignment.Top => "top",
418+
VerticalCellAlignment.Center => "center",
419+
_ => "bottom"
420+
};
410421

422+
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, style1HorizontalAlignment).ConfigureAwait(false);
423+
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, style1VerticalAlignment).ConfigureAwait(false);
424+
await NewWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
425+
411426
var wrapContent = _styleOptions.WrapCellContents ? "1" : "0";
412427
await NewWriter.WriteAttributeStringAsync(null, "wrapText", null, wrapContent).ConfigureAwait(false);
413-
428+
414429
await NewWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
415430
await NewWriter.WriteAttributeStringAsync(null, "relativeIndent", null, "0").ConfigureAwait(false);
416431
await NewWriter.WriteAttributeStringAsync(null, "justifyLastLine", null, "0").ConfigureAwait(false);
@@ -441,8 +456,23 @@ protected override async Task GenerateCellXfAsync()
441456
await NewWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
442457
await NewWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
443458
await NewWriter.WriteStartElementAsync(OldReader.Prefix, "alignment", OldReader.NamespaceURI).ConfigureAwait(false);
444-
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, "general").ConfigureAwait(false);
445-
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
459+
460+
var style2HorizontalAlignment = _styleOptions.HorizontalAlignment switch
461+
{
462+
HorizontalCellAlignment.Center => "center",
463+
HorizontalCellAlignment.Right => "right",
464+
_ => "general"
465+
};
466+
467+
var style2VerticalAlignment = _styleOptions.VerticalAlignment switch
468+
{
469+
VerticalCellAlignment.Top => "top",
470+
VerticalCellAlignment.Center => "center",
471+
_ => "bottom"
472+
};
473+
474+
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, style2HorizontalAlignment).ConfigureAwait(false);
475+
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, style2VerticalAlignment).ConfigureAwait(false);
446476
await NewWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
447477
await NewWriter.WriteAttributeStringAsync(null, "wrapText", null, "0").ConfigureAwait(false);
448478
await NewWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
@@ -499,8 +529,23 @@ protected override async Task GenerateCellXfAsync()
499529
await NewWriter.WriteAttributeStringAsync(null, "applyAlignment", null, "1").ConfigureAwait(false);
500530
await NewWriter.WriteAttributeStringAsync(null, "applyProtection", null, "1").ConfigureAwait(false);
501531
await NewWriter.WriteStartElementAsync(OldReader.Prefix, "alignment", OldReader.NamespaceURI).ConfigureAwait(false);
502-
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, "general").ConfigureAwait(false);
503-
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, "bottom").ConfigureAwait(false);
532+
533+
var style3HorizontalAlignment = _styleOptions.HorizontalAlignment switch
534+
{
535+
HorizontalCellAlignment.Center => "center",
536+
HorizontalCellAlignment.Right => "right",
537+
_ => "general"
538+
};
539+
540+
var style3VerticalAlignment = _styleOptions.VerticalAlignment switch
541+
{
542+
VerticalCellAlignment.Top => "top",
543+
VerticalCellAlignment.Center => "center",
544+
_ => "bottom"
545+
};
546+
547+
await NewWriter.WriteAttributeStringAsync(null, "horizontal", null, style3HorizontalAlignment).ConfigureAwait(false);
548+
await NewWriter.WriteAttributeStringAsync(null, "vertical", null, style3VerticalAlignment).ConfigureAwait(false);
504549
await NewWriter.WriteAttributeStringAsync(null, "textRotation", null, "0").ConfigureAwait(false);
505550
await NewWriter.WriteAttributeStringAsync(null, "wrapText", null, "0").ConfigureAwait(false);
506551
await NewWriter.WriteAttributeStringAsync(null, "indent", null, "0").ConfigureAwait(false);
@@ -516,4 +561,4 @@ protected override async Task GenerateCellXfAsync()
516561
await NewWriter.WriteEndElementAsync().ConfigureAwait(false);
517562
}
518563
}
519-
}
564+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
namespace MiniExcelLib.OpenXml.Styles;
22

3+
using MiniExcelLib.Core.Enums;
4+
35
public class OpenXmlStyleOptions
46
{
57
public bool WrapCellContents { get; set; }
6-
public OpenXmlHeaderStyle? HeaderStyle { get; set; }
8+
public OpenXmlHeaderStyle? HeaderStyle { get; set; }
9+
public HorizontalCellAlignment HorizontalAlignment { get; set; } = HorizontalCellAlignment.Left;
10+
public VerticalCellAlignment VerticalAlignment { get; set; } = VerticalCellAlignment.Bottom;
711
}

0 commit comments

Comments
 (0)