Skip to content

Commit

Permalink
fix #585
Browse files Browse the repository at this point in the history
  • Loading branch information
AZhrZho committed Apr 24, 2024
1 parent 623ecc8 commit abe47ba
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
Binary file added samples/xlsx/TestIssue585.xlsx
Binary file not shown.
22 changes: 20 additions & 2 deletions src/MiniExcel/Csv/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string
_stream.Position = 0;
var reader = _config.StreamReaderFunc(_stream);
{
var row = string.Empty;
string[] read;
var firstRow = true;
Dictionary<int, string> headRows = new Dictionary<int, string>();
while ((row = reader.ReadLine()) != null)
string row;
for (var rowIndex = 1; (row = reader.ReadLine()) != null; rowIndex++)
{
read = Split(row);

// invalid row check
if (read.Length < headRows.Count)
{
var colIndex = read.Length;
var headers = headRows.ToDictionary(x => x.Value, x => x.Key);
var rowValues = read.Select((x, i) => new { Key = headRows[i], Value = x }).ToDictionary(x => x.Key, x => (object)x.Value);
throw new Exceptions.ExcelColumnNotFoundException(null,
headRows[colIndex], null, rowIndex, headers, rowValues, $"Csv read error, Column: {colIndex} not found in Row: {rowIndex}");
}

//header
if (useHeaderRow)
{
Expand All @@ -57,6 +67,14 @@ public IEnumerable<IDictionary<string, object>> Query(bool useHeaderRow, string

//body
{
// record first row as reference
if (firstRow)
{
firstRow = false;
for (int i = 0; i <= read.Length - 1; i++)
headRows.Add(i, $"c{i + 1}");
}

var cell = CustomPropertyHelper.GetEmptyExpandoObject(read.Length - 1, 0);
if (_config.ReadEmptyStringAsNull)
{
Expand Down
25 changes: 3 additions & 22 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
object newV = null;
var columnId = headersDic[alias];
var columnName = keys[columnId];
if (!item.ContainsKey(columnName))
ThrowExcelColumnNotFoundException(pInfo, rowIndex, startCell, headersDic, item);
object itemValue = item[columnName];
item.TryGetValue(columnName, out var itemValue);

if (itemValue == null)
continue;
Expand All @@ -487,17 +485,13 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
object itemValue = null;
if (pInfo.ExcelIndexName != null && keys.Contains(pInfo.ExcelIndexName))
{
if (!item.ContainsKey(pInfo.ExcelIndexName))
ThrowExcelColumnNotFoundException(pInfo, rowIndex, startCell, headersDic, item);
itemValue = item[pInfo.ExcelIndexName];
item.TryGetValue(pInfo.ExcelIndexName, out itemValue);
}
else if (headersDic.ContainsKey(pInfo.ExcelColumnName))
{
var columnId = headersDic[pInfo.ExcelColumnName];
var columnName = keys[columnId];
if (!item.ContainsKey(columnName))
ThrowExcelColumnNotFoundException(pInfo, rowIndex, startCell, headersDic, item);
itemValue = item[columnName];
item.TryGetValue(columnName, out itemValue);
}

if (itemValue == null)
Expand All @@ -511,19 +505,6 @@ private void SetCellsValueAndHeaders(object cellValue, bool useHeaderRow, ref Di
}
}

private static void ThrowExcelColumnNotFoundException(ExcelColumnInfo pInfo, int rowIndex, string startCell, IDictionary<string, int> headers, IDictionary<string, object> row)
{
var columnName = pInfo.ExcelColumnName ?? pInfo.Property.Name;
var errorRow = ReferenceHelper.ConvertCellToXY(startCell).Item2 + rowIndex + 1;
throw new ExcelColumnNotFoundException(pInfo.ExcelIndexName,
pInfo.ExcelColumnName ?? pInfo.Property.Name,
pInfo.ExcelColumnAliases,
ReferenceHelper.ConvertCellToXY(startCell).Item2 + rowIndex + 1,
headers,
row,
$"ColumnName : {columnName}, CellRow : {errorRow}, value of {pInfo.Property.Info.PropertyType.Name} type not found.");
}

private void SetSharedStrings()
{
if (_sharedStrings != null)
Expand Down
38 changes: 38 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3460,5 +3460,43 @@ public void Issue422()

Assert.Equal(1, enumerableWithCount.GetEnumeratorCount);
}

public class Issue20240424VO1
{
public string Col1 { get; set; }
public string Col2 { get; set; }
public string Col3 { get; set; }
}

public class Issue20240424VO2
{
public string Col1 { get; set; }
[ExcelColumnName("Col2")]
public string Prop2 { get; set; }
public string Col3 { get; set; }
}

public class Issue20240424VO3
{
public string Col1 { get; set; }
[ExcelColumnIndex("B")]
public string Prop2 { get; set; }
public string Col3 { get; set; }
}

[Fact]
public void Issue20240424()
{
var path = @"../../../../../samples/xlsx/TestIssue585.xlsx";

var items1 = MiniExcel.Query<Issue20240424VO1>(path);
Assert.Equal(2, items1.Count());

var items2 = MiniExcel.Query<Issue20240424VO2>(path);
Assert.Equal(2, items2.Count());

var items3 = MiniExcel.Query<Issue20240424VO3>(path);
Assert.Equal(2, items3.Count());
}
}
}

0 comments on commit abe47ba

Please sign in to comment.