Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ExcelDataReader/Core/CsvFormat/CsvAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public static void Analyze(Stream stream, char[] separators, Encoding fallbackEn
var average = separatorInfo.SumFieldCount / (double)separatorInfo.RowCount;
var dist = separatorInfo.MaxFieldCount - average;

if (dist < bestDistance)
// If more than one separator has the same number of fields for all sample rows prefer the one with the most fields
if (dist < bestDistance || (dist == bestDistance && dist == 0 && bestSeparatorInfo.MaxFieldCount < separatorInfo.MaxFieldCount))
{
bestDistance = dist;
bestSeparator = separator;
Expand Down
34 changes: 34 additions & 0 deletions test/ExcelDataReader.Tests/ExcelCsvReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,39 @@ public void GitIssue500_QuotedValueWithNewLine()
Assert.That(row2, Is.EqualTo(new object[] { "200212812", "$462.76", "Check", "06/06/2018", "Hash#hash" }));
});
}

[Test]
public void GitIssue463()
{
const string data = """
Name Currency Type Cost "Cost per 1,000 Items"
Test1 ABC XX "10,143.27" 0.00
Test2 EFG YY "10,143.27" 0.00
Test3 IJK ZZ "10,143.27" 0.00

""";

using var reader = ExcelReaderFactory.CreateCsvReader(new MemoryStream(Encoding.UTF8.GetBytes(data)));
reader.Read();
object[] row1 = new object[reader.FieldCount];
reader.GetValues(row1);
reader.Read();
object[] row2 = new object[reader.FieldCount];
reader.GetValues(row2);
reader.Read();
object[] row3 = new object[reader.FieldCount];
reader.GetValues(row3);
reader.Read();
object[] row4 = new object[reader.FieldCount];
reader.GetValues(row4);

Assert.Multiple(() =>
{
Assert.That(row1, Is.EqualTo(new object[] { "Name", "Currency", "Type", "Cost", "Cost per 1,000 Items" }));
Assert.That(row2, Is.EqualTo(new object[] { "Test1", "ABC", "XX", "10,143.27", "0.00" }));
Assert.That(row3, Is.EqualTo(new object[] { "Test2", "EFG", "YY", "10,143.27", "0.00" }));
Assert.That(row4, Is.EqualTo(new object[] { "Test3", "IJK", "ZZ", "10,143.27", "0.00" }));
});
}
}
}