Skip to content

Commit 2027843

Browse files
Merge pull request #893 from michelebastione/fix-issue-888
Fixes #888 by ignoring empty rows on reading
2 parents 0380907 + 085f144 commit 2027843

File tree

6 files changed

+48
-38
lines changed

6 files changed

+48
-38
lines changed
9.92 KB
Binary file not shown.

src/MiniExcel.Core/OpenXml/OpenXmlReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ internal static async Task<OpenXmlReader> CreateAsync(Stream stream, IMiniExcelC
200200

201201
if (rowIndex < startRowIndex)
202202
{
203-
await XmlReaderHelper.ReadFirstContentAsync(reader, cancellationToken)
204-
.ConfigureAwait(false);
205-
await XmlReaderHelper.SkipToNextSameLevelDomAsync(reader, cancellationToken)
206-
.ConfigureAwait(false);
203+
if (await XmlReaderHelper.ReadFirstContentAsync(reader, cancellationToken).ConfigureAwait(false))
204+
{
205+
await XmlReaderHelper.SkipToNextSameLevelDomAsync(reader, cancellationToken).ConfigureAwait(false);
206+
}
207207
continue;
208208
}
209209

tests/MiniExcel.Core.Tests/MiniExcelIssueAsyncTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,10 +1329,8 @@ public async Task Issue153()
13291329
public void Issue137()
13301330
{
13311331
const string path = "../../../../../samples/xlsx/TestIssue137.xlsx";
1332-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it")};
1333-
13341332
{
1335-
var q = _excelImporter.QueryAsync(path, configuration: config).ToBlockingEnumerable();
1333+
var q = _excelImporter.QueryAsync(path).ToBlockingEnumerable();
13361334
var rows = q.ToList();
13371335
var first = rows[0] as IDictionary<string, object>; // https://user-images.githubusercontent.com/12729184/113266322-ba06e400-9307-11eb-9521-d36abfda75cc.png
13381336
Assert.Equal(["A", "B", "C", "D", "E", "F", "G", "H"], first?.Keys.ToArray());
@@ -1368,7 +1366,7 @@ public void Issue137()
13681366

13691367
// dynamic query with head
13701368
{
1371-
var q = _excelImporter.QueryAsync(path, true, configuration: config).ToBlockingEnumerable();
1369+
var q = _excelImporter.QueryAsync(path, true).ToBlockingEnumerable();
13721370
var rows = q.ToList();
13731371
var first = rows[0] as IDictionary<string, object>; // https://user-images.githubusercontent.com/12729184/113266322-ba06e400-9307-11eb-9521-d36abfda75cc.png
13741372
Assert.Equal(["比例", "商品", "滿倉口數", "0", "1為港幣 0為台幣"], first?.Keys.ToArray());
@@ -1390,7 +1388,7 @@ public void Issue137()
13901388
}
13911389

13921390
{
1393-
var q = _excelImporter.QueryAsync<Issue137ExcelRow>(path, configuration: config).ToBlockingEnumerable();
1391+
var q = _excelImporter.QueryAsync<Issue137ExcelRow>(path).ToBlockingEnumerable();
13941392
var rows = q.ToList();
13951393
Assert.Equal(10, rows.Count);
13961394

@@ -1423,10 +1421,8 @@ private class Issue137ExcelRow
14231421
public void Issue138()
14241422
{
14251423
const string path = "../../../../../samples/xlsx/TestIssue138.xlsx";
1426-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
1427-
14281424
{
1429-
var q = _excelImporter.QueryAsync(path, true, configuration: config).ToBlockingEnumerable();
1425+
var q = _excelImporter.QueryAsync(path, true).ToBlockingEnumerable();
14301426
var rows = q.ToList();
14311427
Assert.Equal(6, rows.Count);
14321428

@@ -1452,7 +1448,7 @@ public void Issue138()
14521448
}
14531449
{
14541450

1455-
var q = _excelImporter.QueryAsync<Issue138ExcelRow>(path, configuration: config).ToBlockingEnumerable();
1451+
var q = _excelImporter.QueryAsync<Issue138ExcelRow>(path).ToBlockingEnumerable();
14561452
var rows = q.ToList();
14571453
Assert.Equal(6, rows.Count);
14581454
Assert.Equal(new DateTime(2021, 3, 1), rows[0].Date);

tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,10 +2527,8 @@ public void Issue153()
25272527
public void Issue137()
25282528
{
25292529
const string path = "../../../../../samples/xlsx/TestIssue137.xlsx";
2530-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
2531-
25322530
{
2533-
var rows = _excelImporter.Query(path, configuration: config).ToList();
2531+
var rows = _excelImporter.Query(path).ToList();
25342532
var first = rows[0] as IDictionary<string, object>; // https://user-images.githubusercontent.com/12729184/113266322-ba06e400-9307-11eb-9521-d36abfda75cc.png
25352533
Assert.Equal(["A", "B", "C", "D", "E", "F", "G", "H"], first?.Keys.ToArray());
25362534
Assert.Equal(11, rows.Count);
@@ -2564,7 +2562,7 @@ public void Issue137()
25642562

25652563
// dynamic query with head
25662564
{
2567-
var rows = _excelImporter.Query(path, true, configuration: config).ToList();
2565+
var rows = _excelImporter.Query(path, true).ToList();
25682566
var first = rows[0] as IDictionary<string, object>; //![image](https://user-images.githubusercontent.com/12729184/113266322-ba06e400-9307-11eb-9521-d36abfda75cc.png)
25692567
Assert.Equal(["比例", "商品", "滿倉口數", "0", "1為港幣 0為台幣"], first?.Keys.ToArray());
25702568
Assert.Equal(10, rows.Count);
@@ -2584,7 +2582,7 @@ public void Issue137()
25842582
}
25852583

25862584
{
2587-
var rows = _excelImporter.Query<Issue137ExcelRow>(path, configuration: config).ToList();
2585+
var rows = _excelImporter.Query<Issue137ExcelRow>(path).ToList();
25882586
Assert.Equal(10, rows.Count);
25892587
{
25902588
var row = rows[0];
@@ -2615,11 +2613,8 @@ private class Issue137ExcelRow
26152613
public void Issue138()
26162614
{
26172615
const string path = "../../../../../samples/xlsx/TestIssue138.xlsx";
2618-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("zh") };
2619-
config.Culture.NumberFormat.NumberDecimalSeparator = ",";
2620-
26212616
{
2622-
var rows = _excelImporter.Query(path, true, configuration: config).ToList();
2617+
var rows = _excelImporter.Query(path, true).ToList();
26232618
Assert.Equal(6, rows.Count);
26242619

26252620
foreach (var index in new[] { 0, 2, 5 })
@@ -2644,7 +2639,7 @@ public void Issue138()
26442639
}
26452640
{
26462641

2647-
var rows = _excelImporter.Query<Issue138ExcelRow>(path, configuration: config).ToList();
2642+
var rows = _excelImporter.Query<Issue138ExcelRow>(path).ToList();
26482643
Assert.Equal(6, rows.Count);
26492644
Assert.Equal(new DateTime(2021, 3, 1), rows[0].Date);
26502645

@@ -2774,10 +2769,11 @@ public void Issue422()
27742769
new { Row1 = "1", Row2 = "2" },
27752770
new { Row1 = "3", Row2 = "4" }
27762771
};
2777-
27782772
var enumerableWithCount = new Issue422Enumerable(items);
2773+
27792774
using var path = AutoDeletingPath.Create();
2780-
_excelExporter.Export(path.ToString(), enumerableWithCount);
2775+
_excelExporter.Export(path.ToString(), enumerableWithCount);
2776+
27812777
Assert.Equal(1, enumerableWithCount.GetEnumeratorCount);
27822778
}
27832779

@@ -2855,7 +2851,7 @@ WITH test('Id', 'Name') AS (
28552851
using var reader = cmd.ExecuteReader();
28562852

28572853
using var path = AutoDeletingPath.Create();
2858-
_excelExporter.Export(path.FilePath, reader, configuration: excelconfig, overwriteFile: true);
2854+
_excelExporter.Export(path.FilePath, reader, configuration: excelconfig, overwriteFile: true);
28592855

28602856
var rows = _excelImporter.Query(path.FilePath).ToList();
28612857
Assert.All(rows, x => Assert.Single(x));
@@ -3765,4 +3761,30 @@ public void TestIssue881()
37653761
_ = _excelImporter.Query<Issues409_881>(PathHelper.GetFile("xlsx/TestIssue881.xlsx")).ToList();
37663762
});
37673763
}
3764+
3765+
private record ExcelDataRow(string Key, string Value)
3766+
{
3767+
public ExcelDataRow() : this("", "") { }
3768+
}
3769+
3770+
/// <summary>
3771+
/// https://github.com/mini-software/MiniExcel/issues/888
3772+
/// </summary>
3773+
[Fact]
3774+
public void TestIssue888_ShouldIgnoreFrame()
3775+
{
3776+
var xlsxPath = PathHelper.GetFile("xlsx/Issue888_DataWithFrame.xlsx");
3777+
ExcelDataRow[] dataInSheet =
3778+
[
3779+
new("Key1", "Value1"),
3780+
new("Key2", "Value2")
3781+
];
3782+
3783+
// Act
3784+
using var stream = File.OpenRead(xlsxPath);
3785+
var dataRead = _excelImporter.Query<ExcelDataRow>(stream, startCell: "A2").ToArray();
3786+
3787+
Assert.Equal(dataInSheet, dataRead);
3788+
}
3789+
37683790
}

tests/MiniExcel.Core.Tests/SaveByTemplate/MiniExcelTemplateAsyncTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,7 @@ public async Task TestIEnumerableType()
343343
};
344344
await _excelTemplater.ApplyTemplateAsync(path.ToString(), templatePath, value);
345345

346-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
347-
348-
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString(), configuration: config).ToList();
346+
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString()).ToList();
349347
Assert.Equal(poco.@string, rows[0].@string);
350348
Assert.Equal(poco.@int, rows[0].@int);
351349
Assert.Equal(poco.@double, rows[0].@double);
@@ -409,9 +407,7 @@ public async Task TestTemplateTypeMapping()
409407
};
410408
await _excelTemplater.ApplyTemplateAsync(path.ToString(), templatePath, value);
411409

412-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
413-
414-
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString(), configuration: config).ToList();
410+
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString()).ToList();
415411
Assert.Equal(value.@string, rows[0].@string);
416412
Assert.Equal(value.@int, rows[0].@int);
417413
Assert.Equal(value.@double, rows[0].@double);

tests/MiniExcel.Core.Tests/SaveByTemplate/MiniExcelTemplateTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,7 @@ public void TestIEnumerableType()
519519
};
520520
_excelTemplater.ApplyTemplate(path.ToString(), templatePath, value);
521521

522-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
523-
524-
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString(), configuration: config).ToList();
522+
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString()).ToList();
525523
Assert.Equal(poco.@string, rows[0].@string);
526524
Assert.Equal(poco.@int, rows[0].@int);
527525
Assert.Equal(poco.@double, rows[0].@double);
@@ -589,9 +587,7 @@ public void TestTemplateTypeMapping()
589587
};
590588
_excelTemplater.ApplyTemplate(path.ToString(), templatePath, value);
591589

592-
var config = new OpenXmlConfiguration { Culture = new CultureInfo("it") };
593-
594-
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString(), configuration: config).ToList();
590+
var rows = _excelImporter.Query<TestIEnumerableTypePoco>(path.ToString()).ToList();
595591
Assert.Equal(value.@string, rows[0].@string);
596592
Assert.Equal(value.@int, rows[0].@int);
597593
Assert.Equal(value.@double, rows[0].@double);

0 commit comments

Comments
 (0)