Skip to content

Commit b01e28c

Browse files
committed
[Bug] Fixed MiniExcel.SaveAsByTemplate error when value is List<Dictionary<string, object>> #413
1 parent 45f1142 commit b01e28c

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

docs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222

2323
---
2424

25+
### 1.26.7
26+
- [OPT] Reduce memory allocation when using MemoryStream #427 (via @cupsos)
27+
- [OPT] Add System.Memory pacakge #427 (via @cupsos)
28+
- [OPT] Reduce memory allocation in GetImageFormat() #427 (via @cupsos)
29+
- [Bug] Fixed MiniExcel.SaveAsByTemplate error when value is List<Dictionary<string, object>> #413 (via @shps951023)
30+
2531
### 1.26.6
2632
- [OPT] Template save performance #425 (via @lileyzhao)
2733

docs/README.zh-CN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
---
2727

28+
### 1.26.7
29+
- [OPT] 减少 memory allocation 使用 MemoryStream #427 (via @cupsos)
30+
- [OPT] 添加 System.Memory pacakge #427 (via @cupsos)
31+
- [OPT] 减少 memory allocation in GetImageFormat() #427 (via @cupsos)
32+
- [Bug] 修正 MiniExcel.SaveAsByTemplate value 为 List<Dictionary<string, object>> 异常错误 #413 (via @shps951023)
33+
2834
### 1.26.6
2935
- [OPT] Template save performance #425 (via @lileyzhao)
3036

docs/README.zh-Hant.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424

2525
---
2626

27+
28+
### 1.26.7
29+
- [OPT] 減少 memory allocation 使用 MemoryStream #427 (via @cupsos)
30+
- [OPT] 添加 System.Memory pacakge #427 (via @cupsos)
31+
- [OPT] 減少 memory allocation in GetImageFormat() #427 (via @cupsos)
32+
- [Bug] 修正 MiniExcel.SaveAsByTemplate value 為 List<Dictionary<string, object>> 異常錯誤 #413 (via @shps951023)
33+
2734
### 1.26.6
2835
- [OPT] Template save performance #425 (via @lileyzhao)
2936

samples/xlsx/TestIssue413.xlsx

8.55 KB
Binary file not shown.

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>1.26.6</Version>
4+
<Version>1.26.7</Version>
55
</PropertyGroup>
66
<PropertyGroup>
77
<AssemblyName>MiniExcel</AssemblyName>

src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.Impl.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,20 @@ private void WriteSheetXml(Stream stream, XmlDocument doc, XmlNode sheetData)
389389
private static string ConvertToDateTimeString(KeyValuePair<string, PropInfo> propInfo, object cellValue)
390390
{
391391
string cellValueStr;
392+
393+
392394
//TODO:c.SetAttribute("t", "d"); and custom format
393-
var format = propInfo.Value.PropertyInfo.GetAttributeValue((ExcelFormatAttribute x) => x.Format)
394-
?? propInfo.Value.PropertyInfo.GetAttributeValue((ExcelColumnAttribute x) => x.Format)
395-
?? "yyyy-MM-dd HH:mm:ss";
395+
var format = string.Empty;
396+
if (propInfo.Value.PropertyInfo == null)
397+
{
398+
format = "yyyy-MM-dd HH:mm:ss";
399+
}
400+
else
401+
{
402+
format = propInfo.Value.PropertyInfo.GetAttributeValue((ExcelFormatAttribute x) => x.Format)
403+
?? propInfo.Value.PropertyInfo.GetAttributeValue((ExcelColumnAttribute x) => x.Format)
404+
?? "yyyy-MM-dd HH:mm:ss";
405+
}
396406

397407
cellValueStr = (cellValue as DateTime?)?.ToString(format);
398408
return cellValueStr;

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ public MiniExcelIssueTests(ITestOutputHelper output)
3434
this.output = output;
3535
}
3636

37+
/// <summary>
38+
/// [ · Issue #413 · MiniExcel/MiniExcel]
39+
/// (https://github.com/MiniExcel/MiniExcel/issues/413)
40+
/// </summary>
41+
[Fact]
42+
public void TestIssue413()
43+
{
44+
var path = PathHelper.GetTempFilePath();
45+
var value = new {
46+
list = new List<Dictionary<string,object>>{
47+
new Dictionary<string, object>{ { "id","001"},{ "time",new DateTime(2022,12,25)} },
48+
new Dictionary<string, object>{ { "id","002"},{ "time",new DateTime(2022,9,23)} },
49+
}
50+
};
51+
var templatePath = PathHelper.GetFile("xlsx/TestIssue413.xlsx");
52+
MiniExcel.SaveAsByTemplate(path,templatePath, value);
53+
var rows = MiniExcel.Query(path).ToList();
54+
Assert.Equal("2022-12-25 00:00:00", rows[1].B);
55+
Assert.Equal("2022-09-23 00:00:00", rows[2].B);
56+
}
57+
3758
/// <summary>
3859
/// [SaveAs Support empty sharedstring · Issue #405 · MiniExcel/MiniExcel]
3960
/// (https://github.com/MiniExcel/MiniExcel/issues/405)

0 commit comments

Comments
 (0)