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
8 changes: 5 additions & 3 deletions src/MiniExcel/SaveByTemplate/ExcelOpenXmlTemplate.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
}

var cellValue = inputMaps[propNames[0]]; // 1. From left to right, only the first set is used as the basis for the list
if ((cellValue is IEnumerable || cellValue is IList<object>) && !(cellValue is string))
if (cellValue is IEnumerable && !(cellValue is string))
{
if (this.XMergeCellInfos.ContainsKey(r))
{
Expand All @@ -987,7 +987,9 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
}

xRowInfo.CellIEnumerableValues = cellValue as IEnumerable;
xRowInfo.CellIlListValues = cellValue as IList<object>;
xRowInfo.CellIlListValues = cellValue is IList<object> ?
cellValue as IList<object> :
xRowInfo.CellIEnumerableValues.Cast<object>().ToList();

// get ienumerable runtime type
if (xRowInfo.IEnumerableGenricType == null) //avoid duplicate to add rowindexdiff ![image](https://user-images.githubusercontent.com/12729184/114851348-522ac000-9e14-11eb-8244-4730754d6885.png)
Expand Down Expand Up @@ -1283,4 +1285,4 @@ private static bool EvaluateStatement(object tagValue, string comparisonOperator
return checkStatement;
}
}
}
}
60 changes: 60 additions & 0 deletions tests/MiniExcelTests/SaveByTemplate/MiniExcelTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,66 @@ public void DictionaryTemplateTest()
}
}

private struct Employee
{
public string name { get; set; }
public string department { get; set; }
};

[Fact]
public void GroupTemplateTest()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");
var templatePath = @"../../../../../samples/xlsx/TestTemplateBasicIEmumerableFillGroup.xlsx";
var value = new
{
employees = new List<Employee>()
{
new(){ name = "Jack", department="HR" },
new(){ name = "Jack", department="IT" },
new(){ name = "Loan", department="IT" },
new(){ name = "Eric", department="IT" },
new(){ name = "Eric", department="HR" },
new(){ name = "Keaton", department="IT" },
new(){ name = "Felix", department="HR" },
},
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);

{
var rows = MiniExcel.Query(path).ToList();

Assert.Equal(16, rows.Count);

Assert.Equal("Jack", rows[1].A);
Assert.Equal("Jack", rows[2].A);
Assert.Equal("HR", rows[2].B);
Assert.Equal("Jack", rows[3].A);
Assert.Equal("IT", rows[3].B);

Assert.Equal("Loan", rows[4].A);
Assert.Equal("Loan", rows[5].A);
Assert.Equal("IT", rows[5].B);

Assert.Equal("Eric", rows[6].A);
Assert.Equal("Eric", rows[7].A);
Assert.Equal("IT", rows[7].B);
Assert.Equal("Eric", rows[8].A);
Assert.Equal("HR", rows[8].B);

Assert.Equal("Keaton", rows[9].A);
Assert.Equal("Keaton", rows[10].A);
Assert.Equal("IT", rows[10].B);

Assert.Equal("Felix", rows[11].A);
Assert.Equal("Felix", rows[12].A);
Assert.Equal("HR", rows[12].B);

var demension = Helpers.GetFirstSheetDimensionRefValue(path);
Assert.Equal("A1:B20", demension);
}
}

[Fact]
public void TestGithubProject()
{
Expand Down
Loading