Skip to content

Commit 972663f

Browse files
authored
fix(MiniExcelDataReader): GetOrdinal always returns 0 (#711)
1 parent bb34116 commit 972663f

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

src/MiniExcel/MiniExcelDataReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public override string GetName(int i)
6666
return _keys[i];
6767
}
6868

69+
/// <inheritdoc/>
70+
public override int GetOrdinal(string name)
71+
{
72+
_keys.IndexOf(name);
73+
return _keys.IndexOf(name);
74+
}
75+
6976
/// <inheritdoc/>
7077
protected override void Dispose(bool disposing)
7178
{

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void TestIssue549()
4747
};
4848
var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
4949
MiniExcel.SaveAs(path, data);
50-
var rows = MiniExcel.Query(path,true).ToList();
50+
var rows = MiniExcel.Query(path, true).ToList();
5151
{
5252
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
5353
{
@@ -3536,7 +3536,8 @@ class Issue507V01
35363536
public int D { get; set; }
35373537
}
35383538

3539-
class Issue507V02 {
3539+
class Issue507V02
3540+
{
35403541
public DateTime B { get; set; }
35413542
public int D { get; set; }
35423543
}
@@ -3547,7 +3548,7 @@ public void Issue507_1()
35473548
//Problem with multi-line when using Query func
35483549
//https://github.com/mini-software/MiniExcel/issues/507
35493550

3550-
var path = Path.Combine(Path.GetTempPath(), string.Concat( nameof(MiniExcelIssueTests),"_", nameof(Issue507_1), ".csv" ));
3551+
var path = Path.Combine(Path.GetTempPath(), string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue507_1), ".csv"));
35513552
var values = new Issue507V01[]
35523553
{
35533554
new() { A = "Github", B = DateTime.Parse("2021-01-01"), C = "abcd", D = 123 },
@@ -3570,9 +3571,9 @@ public void Issue507_1()
35703571

35713572
// read
35723573
var getRowsInfo = MiniExcel.Query<Issue507V01>(path, excelType: ExcelType.CSV, configuration: config).ToArray();
3573-
3574+
35743575
Assert.Equal(values.Length, getRowsInfo.Count());
3575-
3576+
35763577
Assert.Equal("Github", getRowsInfo[0].A);
35773578
Assert.Equal("abcd", getRowsInfo[0].C);
35783579

@@ -3587,7 +3588,8 @@ public void Issue507_1()
35873588
}
35883589

35893590
[Fact]
3590-
public void Issue507_2() {
3591+
public void Issue507_2()
3592+
{
35913593
//Problem with multi-line when using Query func
35923594
//https://github.com/mini-software/MiniExcel/issues/507
35933595

@@ -3620,7 +3622,8 @@ public void Issue507_2() {
36203622
}
36213623

36223624
[Fact]
3623-
public void Issue507_3_MismatchedQuoteCsv() {
3625+
public void Issue507_3_MismatchedQuoteCsv()
3626+
{
36243627
//Problem with multi-line when using Query func
36253628
//https://github.com/mini-software/MiniExcel/issues/507
36263629

@@ -3633,12 +3636,12 @@ public void Issue507_3_MismatchedQuoteCsv() {
36333636
var badCsv = "A,B,C\n\"r1a: no end quote,r1b,r1c";
36343637

36353638
// create
3636-
using var stream = new MemoryStream( Encoding.UTF8.GetBytes( badCsv ) );
3639+
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(badCsv));
36373640

36383641
// read
3639-
var getRowsInfo = MiniExcel.Query( stream, excelType: ExcelType.CSV, configuration: config ).ToArray();
3642+
var getRowsInfo = MiniExcel.Query(stream, excelType: ExcelType.CSV, configuration: config).ToArray();
36403643

3641-
Assert.Equal(2, getRowsInfo.Length );
3644+
Assert.Equal(2, getRowsInfo.Length);
36423645

36433646
}
36443647

@@ -3655,8 +3658,8 @@ public void Issue606_1()
36553658
{
36563659
Title = "My Title",
36573660
OrderInfo = Enumerable
3658-
.Range( 1, 100 )
3659-
.Select( x => new
3661+
.Range(1, 100)
3662+
.Select(x => new
36603663
{
36613664
Standard = "standard",
36623665
RegionName = "region",
@@ -3682,13 +3685,13 @@ public void Issue606_1()
36823685
var path = Path.Combine
36833686
(
36843687
Path.GetTempPath(),
3685-
string.Concat( nameof( MiniExcelIssueTests ), "_", nameof( Issue606_1 ), ".xlsx" )
3688+
string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue606_1), ".xlsx")
36863689
);
36873690

36883691
var templateFileName = @"../../../../../samples/xlsx/TestIssue606_Template.xlsx";
36893692

36903693

3691-
MiniExcel.SaveAsByTemplate( path, Path.GetFullPath( templateFileName ), value );
3694+
MiniExcel.SaveAsByTemplate(path, Path.GetFullPath(templateFileName), value);
36923695

36933696
}
36943697

@@ -3698,7 +3701,8 @@ public void Issue632_1()
36983701
//https://github.com/mini-software/MiniExcel/issues/632
36993702
var values = new List<Dictionary<string, object>>();
37003703

3701-
foreach ( var item in Enumerable.Range( 1, 100 ) ) {
3704+
foreach (var item in Enumerable.Range(1, 100))
3705+
{
37023706
var dict = new Dictionary<string, object>
37033707
{
37043708
{ "Id", item },
@@ -3711,7 +3715,7 @@ public void Issue632_1()
37113715
{ "Network Usage (Kb/s)", Math.Round( 4503.23422, 1 ) },
37123716
{ "Instrument", "QT800050" }
37133717
};
3714-
values.Add( dict );
3718+
values.Add(dict);
37153719
}
37163720

37173721
var config = new OpenXmlConfiguration
@@ -3728,11 +3732,11 @@ public void Issue632_1()
37283732

37293733
var path = Path.Combine(
37303734
Path.GetTempPath(),
3731-
string.Concat( nameof( MiniExcelIssueTests ), "_", nameof( Issue632_1 ), ".xlsx" )
3735+
string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue632_1), ".xlsx")
37323736
);
3733-
3734-
MiniExcel.SaveAs( path, values, excelType: ExcelType.XLSX, configuration: config, overwriteFile: true );
3735-
3737+
3738+
MiniExcel.SaveAs(path, values, excelType: ExcelType.XLSX, configuration: config, overwriteFile: true);
3739+
37363740
}
37373741

37383742
private class Issue658TestData
@@ -3811,5 +3815,32 @@ static IEnumerable<Issue658TestData> GetTestData()
38113815
i++;
38123816
}
38133817
}
3818+
3819+
[Fact]
3820+
public void Issue_710()
3821+
{
3822+
var values = new[] { new { Column1 = "MiniExcel", Column2 = 1, Column3 = "Test" } };
3823+
using var memoryStream = new MemoryStream();
3824+
memoryStream.SaveAs(values, configuration: new OpenXmlConfiguration
3825+
{
3826+
FastMode = true,
3827+
});
3828+
3829+
memoryStream.Position = 0;
3830+
3831+
var dataReader = memoryStream.GetReader(useHeaderRow: false);
3832+
3833+
dataReader.Read();
3834+
{
3835+
for (int i = 0; i < dataReader.FieldCount; i++)
3836+
{
3837+
var columnName = dataReader.GetName(i);
3838+
3839+
var ordinal = dataReader.GetOrdinal(columnName);
3840+
3841+
Assert.Equal(i, ordinal);
3842+
}
3843+
}
3844+
}
38143845
}
38153846
}

0 commit comments

Comments
 (0)