Skip to content

Commit 6408300

Browse files
committed
Read vertical alignment
Fixes issue #574
1 parent 47a5eaa commit 6408300

File tree

12 files changed

+73
-8
lines changed

12 files changed

+73
-8
lines changed

src/ExcelDataReader.Tests/ExcelTestBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,17 @@ public void GitIssue694ExcelTimeFormatTimeSpan()
980980
Assert.That(TimeSpan.Parse(reader[1].ToString()), Is.EqualTo(TimeSpan.Parse("00:11")));
981981
}
982982

983+
[Test]
984+
public void GitIssue574VerticalAlignment()
985+
{
986+
using var reader = OpenReader("Test_git_issue_574");
987+
reader.Read();
988+
989+
Assert.That(reader.GetCellStyle(0).VerticalAlignment, Is.EqualTo(VerticalAlignment.Top));
990+
Assert.That(reader.GetCellStyle(1).VerticalAlignment, Is.EqualTo(VerticalAlignment.Center));
991+
Assert.That(reader.GetCellStyle(2).VerticalAlignment, Is.EqualTo(VerticalAlignment.Bottom));
992+
}
993+
983994
protected IExcelDataReader OpenReader(string name)
984995
{
985996
return OpenReader(OpenStream(name));

src/ExcelDataReader/CellStyle.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ public enum HorizontalAlignment
4646
Distributed,
4747
}
4848

49+
/// <summary>
50+
/// Vertical alignment.
51+
/// </summary>
52+
public enum VerticalAlignment
53+
{
54+
/// <summary>
55+
/// Top.
56+
/// </summary>
57+
Top,
58+
59+
/// <summary>
60+
/// Center.
61+
/// </summary>
62+
Center,
63+
64+
/// <summary>
65+
/// Bottom.
66+
/// </summary>
67+
Bottom,
68+
69+
/// <summary>
70+
/// Justify.
71+
/// </summary>
72+
Justify,
73+
74+
/// <summary>
75+
/// Distributed.
76+
/// </summary>
77+
Distributed,
78+
}
79+
4980
/// <summary>
5081
/// Holds style information for a cell.
5182
/// </summary>
@@ -71,6 +102,11 @@ public class CellStyle
71102
/// </summary>
72103
public HorizontalAlignment HorizontalAlignment { get; internal set; }
73104

105+
/// <summary>
106+
/// Gets the vertical alignment.
107+
/// </summary>
108+
public VerticalAlignment VerticalAlignment { get; internal set; }
109+
74110
/// <summary>
75111
/// Gets a value indicating whether the cell is hidden.
76112
/// </summary>

src/ExcelDataReader/Core/BinaryFormat/XlsBiffXF.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ internal XlsBiffXF(byte[] bytes, int biffVersion)
3131
IsHidden = (ReadByte(2) & 2) != 0;
3232
IsCellStyleXf = (ReadByte(2) & 4) != 0;
3333
ParentCellStyleXf = ReadUInt16(2) >> 4;
34-
HorizontalAlignment = (HorizontalAlignment)(ReadByte(4) & 0x07);
34+
HorizontalAlignment = (HorizontalAlignment)(ReadByte(4) & 0b111);
35+
VerticalAlignment = (VerticalAlignment)(ReadByte(4) >> 4 & 0b111);
3536
break;
3637
default:
3738
Font = ReadUInt16(0);
@@ -40,7 +41,8 @@ internal XlsBiffXF(byte[] bytes, int biffVersion)
4041
IsHidden = (ReadByte(4) & 2) != 0;
4142
IsCellStyleXf = (ReadByte(4) & 4) != 0;
4243
ParentCellStyleXf = ReadUInt16(4) >> 4;
43-
HorizontalAlignment = (HorizontalAlignment)(ReadByte(6) & 0x07);
44+
HorizontalAlignment = (HorizontalAlignment)(ReadByte(6) & 0b111);
45+
VerticalAlignment = (VerticalAlignment)(ReadByte(6) >> 4 & 0b111);
4446
if (biffVersion == 8)
4547
{
4648
IndentLevel = ReadByte(8) & 0x0F;
@@ -79,4 +81,6 @@ internal XlsBiffXF(byte[] bytes, int biffVersion)
7981
public int IndentLevel { get; }
8082

8183
public HorizontalAlignment HorizontalAlignment { get; }
84+
85+
public VerticalAlignment VerticalAlignment { get; }
8286
}

src/ExcelDataReader/Core/BinaryFormat/XlsWorkbook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public IEnumerable<XlsWorksheet> ReadWorksheets()
137137

138138
internal void AddXf(XlsBiffXF xf)
139139
{
140-
var extendedFormat = new ExtendedFormat(xf.ParentCellStyleXf, xf.Font, xf.Format, xf.IsLocked, xf.IsHidden, xf.IndentLevel, xf.HorizontalAlignment);
140+
var extendedFormat = new ExtendedFormat(xf.ParentCellStyleXf, xf.Font, xf.Format, xf.IsLocked, xf.IsHidden, xf.IndentLevel, xf.HorizontalAlignment, xf.VerticalAlignment);
141141

142142
// The workbook holds two kinds of XF records: Cell XFs, and Cell Style XFs.
143143
// In the binary XLS format, both kinds of XF records are saved in a single list,

src/ExcelDataReader/Core/ExtendedFormat.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public ExtendedFormat(int numberFormatIndex)
99
NumberFormatIndex = numberFormatIndex;
1010
}
1111

12-
public ExtendedFormat(int parentCellStyleXf, int fontIndex, int numberFormatIndex, bool locked, bool hidden, int indentLevel, HorizontalAlignment horizontalAlignment)
12+
public ExtendedFormat(int parentCellStyleXf, int fontIndex, int numberFormatIndex, bool locked, bool hidden, int indentLevel, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
1313
{
1414
ParentCellStyleXf = parentCellStyleXf;
1515
FontIndex = fontIndex;
@@ -18,6 +18,7 @@ public ExtendedFormat(int parentCellStyleXf, int fontIndex, int numberFormatInde
1818
Hidden = hidden;
1919
IndentLevel = indentLevel;
2020
HorizontalAlignment = horizontalAlignment;
21+
VerticalAlignment = verticalAlignment;
2122
}
2223

2324
private ExtendedFormat()
@@ -43,4 +44,6 @@ private ExtendedFormat()
4344
public int IndentLevel { get; }
4445

4546
public HorizontalAlignment HorizontalAlignment { get; }
47+
48+
public VerticalAlignment VerticalAlignment { get; }
4649
}

src/ExcelDataReader/Core/OpenXmlFormat/BinaryFormat/BiffStylesReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected override Record ReadOverride(byte[] buffer, uint recordId, uint record
5454
(buffer[13] & 0x10000) != 0,
5555
(buffer[13] & 0x100000) != 0,
5656
(int)(uint)buffer[11],
57-
(HorizontalAlignment)(buffer[12] & 0b111));
57+
(HorizontalAlignment)(buffer[12] & 0b111),
58+
(VerticalAlignment)(buffer[12] >> 3 & 0b111));
5859

5960
if (_inCellXf)
6061
return new ExtendedFormatRecord(extendedFormat);

src/ExcelDataReader/Core/OpenXmlFormat/XmlFormat/XmlStylesReader.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal sealed class XmlStylesReader(XmlReader reader) : XmlRecordReader(reader
2626
private const string AIndent = "indent";
2727
private const string AHorizontal = "horizontal";
2828

29+
private const string AVertical = "vertical";
30+
2931
private const string NProtection = "protection";
3032
private const string AHidden = "hidden";
3133
private const string ALocked = "locked";
@@ -101,9 +103,9 @@ private IEnumerable<ExtendedFormat> ReadCellXfs(string nsSpreadsheetMl)
101103
// var applyNumberFormat = Reader.GetAttribute(AApplyNumberFormat) == "1";
102104
// var applyAlignment = Reader.GetAttribute(AApplyAlignment) == "1";
103105
// var applyProtection = Reader.GetAttribute(AApplyProtection) == "1";
104-
ReadAlignment(Reader, nsSpreadsheetMl, out int indentLevel, out HorizontalAlignment horizontalAlignment, out var hidden, out var locked);
106+
ReadAlignment(Reader, nsSpreadsheetMl, out int indentLevel, out HorizontalAlignment horizontalAlignment, out var verticalAlignment, out var hidden, out var locked);
105107

106-
yield return new ExtendedFormat(xfId, -1, numFmtId, locked, hidden, indentLevel, horizontalAlignment);
108+
yield return new ExtendedFormat(xfId, -1, numFmtId, locked, hidden, indentLevel, horizontalAlignment, verticalAlignment);
107109

108110
// reader.Skip();
109111
}
@@ -113,10 +115,11 @@ private IEnumerable<ExtendedFormat> ReadCellXfs(string nsSpreadsheetMl)
113115
}
114116
}
115117

116-
static void ReadAlignment(XmlReader reader, string nsSpreadsheetMl, out int indentLevel, out HorizontalAlignment horizontalAlignment, out bool hidden, out bool locked)
118+
static void ReadAlignment(XmlReader reader, string nsSpreadsheetMl, out int indentLevel, out HorizontalAlignment horizontalAlignment, out VerticalAlignment verticalAlignment, out bool hidden, out bool locked)
117119
{
118120
indentLevel = 0;
119121
horizontalAlignment = HorizontalAlignment.General;
122+
verticalAlignment = VerticalAlignment.Bottom;
120123
hidden = false;
121124
locked = false;
122125

@@ -137,6 +140,12 @@ static void ReadAlignment(XmlReader reader, string nsSpreadsheetMl, out int inde
137140
Enum.TryParse(attrValue, true, out horizontalAlignment);
138141
}
139142

143+
attrValue = reader.GetAttribute(AVertical);
144+
if (attrValue is not null)
145+
{
146+
Enum.TryParse(attrValue, true, out verticalAlignment);
147+
}
148+
140149
reader.Skip();
141150
}
142151
else if (reader.IsStartElement(NProtection, nsSpreadsheetMl))

src/ExcelDataReader/ExcelDataReader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public CellStyle GetCellStyle(int i)
194194
result.NumberFormatIndex = effectiveStyle.NumberFormatIndex;
195195
result.IndentLevel = effectiveStyle.IndentLevel;
196196
result.HorizontalAlignment = effectiveStyle.HorizontalAlignment;
197+
result.VerticalAlignment = effectiveStyle.VerticalAlignment;
197198
result.Hidden = effectiveStyle.Hidden;
198199
result.Locked = effectiveStyle.Locked;
199200
return result;
25.5 KB
Binary file not shown.
7.98 KB
Binary file not shown.

0 commit comments

Comments
 (0)