Skip to content

Commit

Permalink
Fix column count issue with xlsx reader moves to new worksheet. (#161)
Browse files Browse the repository at this point in the history
* Fix for github issue 160: xlsx reader doesn't reset column state properly when advancing to new worksheet.

* Update release notes and bump version
  • Loading branch information
MarkPflug authored Apr 12, 2024
1 parent 9557ae8 commit 968620f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Sylvan.Data.Excel Release Notes

_0.4.20_
- Fixes a bug where advancing to the next worksheet can result in the incorrect number of columns being reported when reading xlsx files.

_0.4.19_
- Fix a bug where headers might not be read properly on sheets after the first sheet.

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
47 changes: 47 additions & 0 deletions source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,53 @@ public void ChartSheet()
Assert.Equal("Sheet1", edr.WorksheetName);
}

[Fact]
public void MultiSheetHeader()
{
var file = GetFile("MultiSheet2");
using var edr = ExcelDataReader.Create(file);
edr.NextResult();
edr.NextResult();
edr.Read();
Assert.Equal(5, edr.FieldCount);
for (int i = 0; i < edr.FieldCount; i++)
{
var expected = "" + (char)('v' + i);
Assert.Equal(expected, edr.GetName(i));
}

for (int i = 0; i < edr.FieldCount; i++)
{
var expected = "" + (char)('1' + i);
Assert.Equal(expected, edr.GetString(i));
}
}

[Fact]
public void MultiSheetNoHeader()
{
var opt = new ExcelDataReaderOptions { Schema = ExcelSchema.NoHeaders };
var file = GetFile("MultiSheet2");
using var edr = ExcelDataReader.Create(file, opt);

edr.NextResult();
edr.NextResult();
edr.Read();
Assert.Equal(5, edr.FieldCount);
// in no-headers mode the column names will be the Excel column names: A, B, C .. AA, AB, etc
for (int i = 0; i < edr.FieldCount; i++)
{
var expected = "" + (char)('A' + i);
Assert.Equal(expected, edr.GetName(i));
}

for (int i = 0; i < edr.FieldCount; i++)
{
var expected = "" + (char)('v' + i);
Assert.Equal(expected, edr.GetString(i));
}
}

#if ASYNC

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<VersionPrefix>0.4.19</VersionPrefix>
<VersionPrefix>0.4.20</VersionPrefix>
<VersionSuffix>b0001</VersionSuffix>
<Description>A cross-platform .NET library for reading Excel data files.</Description>
<PackageTags>excel;xls;xlsx;xlsb;datareader</PackageTags>
<Nullable>enable</Nullable>
Expand Down
5 changes: 5 additions & 0 deletions source/Sylvan.Data.Excel/Xlsx/XlsxWorkbookReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private protected override bool OpenWorksheet(int sheetIdx)

this.reader = XmlReader.Create(tr, settings);
this.rowIndex = 0;
this.rowFieldCount = this.curFieldCount = 0;
// worksheet
while (reader.Read())
{
Expand Down Expand Up @@ -292,6 +293,10 @@ bool InitializeSheet()
this.curFieldCount = rowFieldCount;
this.rowFieldCount = 0;
}
else
{
this.curFieldCount = rowFieldCount;
}

if (LoadSchema())
{
Expand Down

0 comments on commit 968620f

Please sign in to comment.