From 972663f8154720039e642e74c6d9fccba741c381 Mon Sep 17 00:00:00 2001 From: Amos Date: Sun, 19 Jan 2025 12:34:57 +0800 Subject: [PATCH] fix(MiniExcelDataReader): GetOrdinal always returns 0 (#711) --- src/MiniExcel/MiniExcelDataReader.cs | 7 ++ tests/MiniExcelTests/MiniExcelIssueTests.cs | 71 +++++++++++++++------ 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/MiniExcel/MiniExcelDataReader.cs b/src/MiniExcel/MiniExcelDataReader.cs index f2ae9a7..5d52517 100644 --- a/src/MiniExcel/MiniExcelDataReader.cs +++ b/src/MiniExcel/MiniExcelDataReader.cs @@ -66,6 +66,13 @@ public override string GetName(int i) return _keys[i]; } + /// + public override int GetOrdinal(string name) + { + _keys.IndexOf(name); + return _keys.IndexOf(name); + } + /// protected override void Dispose(bool disposing) { diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index d8da929..2ddc489 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -47,7 +47,7 @@ public void TestIssue549() }; var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx"; MiniExcel.SaveAs(path, data); - var rows = MiniExcel.Query(path,true).ToList(); + var rows = MiniExcel.Query(path, true).ToList(); { using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { @@ -3536,7 +3536,8 @@ class Issue507V01 public int D { get; set; } } - class Issue507V02 { + class Issue507V02 + { public DateTime B { get; set; } public int D { get; set; } } @@ -3547,7 +3548,7 @@ public void Issue507_1() //Problem with multi-line when using Query func //https://github.com/mini-software/MiniExcel/issues/507 - var path = Path.Combine(Path.GetTempPath(), string.Concat( nameof(MiniExcelIssueTests),"_", nameof(Issue507_1), ".csv" )); + var path = Path.Combine(Path.GetTempPath(), string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue507_1), ".csv")); var values = new Issue507V01[] { new() { A = "Github", B = DateTime.Parse("2021-01-01"), C = "abcd", D = 123 }, @@ -3570,9 +3571,9 @@ public void Issue507_1() // read var getRowsInfo = MiniExcel.Query(path, excelType: ExcelType.CSV, configuration: config).ToArray(); - + Assert.Equal(values.Length, getRowsInfo.Count()); - + Assert.Equal("Github", getRowsInfo[0].A); Assert.Equal("abcd", getRowsInfo[0].C); @@ -3587,7 +3588,8 @@ public void Issue507_1() } [Fact] - public void Issue507_2() { + public void Issue507_2() + { //Problem with multi-line when using Query func //https://github.com/mini-software/MiniExcel/issues/507 @@ -3620,7 +3622,8 @@ public void Issue507_2() { } [Fact] - public void Issue507_3_MismatchedQuoteCsv() { + public void Issue507_3_MismatchedQuoteCsv() + { //Problem with multi-line when using Query func //https://github.com/mini-software/MiniExcel/issues/507 @@ -3633,12 +3636,12 @@ public void Issue507_3_MismatchedQuoteCsv() { var badCsv = "A,B,C\n\"r1a: no end quote,r1b,r1c"; // create - using var stream = new MemoryStream( Encoding.UTF8.GetBytes( badCsv ) ); + using var stream = new MemoryStream(Encoding.UTF8.GetBytes(badCsv)); // read - var getRowsInfo = MiniExcel.Query( stream, excelType: ExcelType.CSV, configuration: config ).ToArray(); + var getRowsInfo = MiniExcel.Query(stream, excelType: ExcelType.CSV, configuration: config).ToArray(); - Assert.Equal(2, getRowsInfo.Length ); + Assert.Equal(2, getRowsInfo.Length); } @@ -3655,8 +3658,8 @@ public void Issue606_1() { Title = "My Title", OrderInfo = Enumerable - .Range( 1, 100 ) - .Select( x => new + .Range(1, 100) + .Select(x => new { Standard = "standard", RegionName = "region", @@ -3682,13 +3685,13 @@ public void Issue606_1() var path = Path.Combine ( Path.GetTempPath(), - string.Concat( nameof( MiniExcelIssueTests ), "_", nameof( Issue606_1 ), ".xlsx" ) + string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue606_1), ".xlsx") ); var templateFileName = @"../../../../../samples/xlsx/TestIssue606_Template.xlsx"; - MiniExcel.SaveAsByTemplate( path, Path.GetFullPath( templateFileName ), value ); + MiniExcel.SaveAsByTemplate(path, Path.GetFullPath(templateFileName), value); } @@ -3698,7 +3701,8 @@ public void Issue632_1() //https://github.com/mini-software/MiniExcel/issues/632 var values = new List>(); - foreach ( var item in Enumerable.Range( 1, 100 ) ) { + foreach (var item in Enumerable.Range(1, 100)) + { var dict = new Dictionary { { "Id", item }, @@ -3711,7 +3715,7 @@ public void Issue632_1() { "Network Usage (Kb/s)", Math.Round( 4503.23422, 1 ) }, { "Instrument", "QT800050" } }; - values.Add( dict ); + values.Add(dict); } var config = new OpenXmlConfiguration @@ -3728,11 +3732,11 @@ public void Issue632_1() var path = Path.Combine( Path.GetTempPath(), - string.Concat( nameof( MiniExcelIssueTests ), "_", nameof( Issue632_1 ), ".xlsx" ) + string.Concat(nameof(MiniExcelIssueTests), "_", nameof(Issue632_1), ".xlsx") ); - - MiniExcel.SaveAs( path, values, excelType: ExcelType.XLSX, configuration: config, overwriteFile: true ); - + + MiniExcel.SaveAs(path, values, excelType: ExcelType.XLSX, configuration: config, overwriteFile: true); + } private class Issue658TestData @@ -3811,5 +3815,32 @@ static IEnumerable GetTestData() i++; } } + + [Fact] + public void Issue_710() + { + var values = new[] { new { Column1 = "MiniExcel", Column2 = 1, Column3 = "Test" } }; + using var memoryStream = new MemoryStream(); + memoryStream.SaveAs(values, configuration: new OpenXmlConfiguration + { + FastMode = true, + }); + + memoryStream.Position = 0; + + var dataReader = memoryStream.GetReader(useHeaderRow: false); + + dataReader.Read(); + { + for (int i = 0; i < dataReader.FieldCount; i++) + { + var columnName = dataReader.GetName(i); + + var ordinal = dataReader.GetOrdinal(columnName); + + Assert.Equal(i, ordinal); + } + } + } } } \ No newline at end of file