Skip to content

Commit ceff56f

Browse files
Added support for DateOnly type in query mapping
2 parents 5aa63a9 + 56b62a2 commit ceff56f

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

samples/xlsx/TestDateOnlyMapping.xlsx

6.84 KB
Binary file not shown.

src/MiniExcel/Utils/TypeHelper.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
8080
}
8181
else if (pInfo.ExcludeNullableType == typeof(Guid))
8282
{
83-
newValue = Guid.Parse(itemValue.ToString());
83+
newValue = Guid.Parse(itemValue.ToString() ?? Guid.Empty.ToString());
8484
}
8585
else if (pInfo.ExcludeNullableType == typeof(DateTimeOffset))
8686
{
@@ -100,7 +100,7 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
100100
else if (pInfo.ExcludeNullableType == typeof(DateTime))
101101
{
102102
// fix issue 257 https://github.com/shps951023/MiniExcel/issues/257
103-
if (itemValue is DateTime || itemValue is DateTime?)
103+
if (itemValue is DateTime)
104104
{
105105
newValue = itemValue;
106106
pInfo.Property.SetValue(v, newValue);
@@ -128,9 +128,37 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
128128
else
129129
throw new InvalidCastException($"{vs} cannot be cast to DateTime");
130130
}
131+
#if NET6_0_OR_GREATER
132+
else if (pInfo.ExcludeNullableType == typeof(DateOnly))
133+
{
134+
if (itemValue is DateOnly)
135+
{
136+
newValue = itemValue;
137+
pInfo.Property.SetValue(v, newValue);
138+
return newValue;
139+
}
140+
141+
var vs = itemValue?.ToString();
142+
if (pInfo.ExcelFormat != null)
143+
{
144+
if (DateOnly.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
145+
{
146+
newValue = _v;
147+
}
148+
}
149+
else if (DateOnly.TryParse(vs, _config.Culture, DateTimeStyles.None, out var _v))
150+
newValue = _v;
151+
else if (DateOnly.TryParseExact(vs, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2))
152+
newValue = _v2;
153+
else if (double.TryParse(vs, NumberStyles.None, CultureInfo.InvariantCulture, out var _d))
154+
newValue = DateOnly.FromDateTime(DateTime.FromOADate(_d));
155+
else
156+
throw new InvalidCastException($"{vs} cannot be cast to DateOnly");
157+
}
158+
#endif
131159
else if (pInfo.ExcludeNullableType == typeof(TimeSpan))
132160
{
133-
if (itemValue is TimeSpan || itemValue is TimeSpan?)
161+
if (itemValue is TimeSpan)
134162
{
135163
newValue = itemValue;
136164
pInfo.Property.SetValue(v, newValue);

tests/MiniExcelTests/MiniExcelOpenXmlTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,4 +1524,24 @@ public void InsertSheetTest()
15241524
Assert.True(sheet3.Name == "Sheet3");
15251525
}
15261526
}
1527+
1528+
private class DateOnlyTest
1529+
{
1530+
public DateOnly Date { get; set; }
1531+
[ExcelFormat("d.M.yyyy")] public DateOnly DateWithFormat { get; set; }
1532+
}
1533+
1534+
[Fact]
1535+
public void DateOnlySupportTest()
1536+
{
1537+
var query = MiniExcel.Query<DateOnlyTest>(PathHelper.GetFile("xlsx/TestDateOnlyMapping.xlsx")).ToList();
1538+
1539+
Assert.Equal(new DateOnly(2020, 9, 27), query[0].Date);
1540+
Assert.Equal(new DateOnly(2020, 10, 25), query[1].Date);
1541+
Assert.Equal(new DateOnly(2021, 10, 4), query[2].Date);
1542+
1543+
Assert.Equal(new DateOnly(2020, 9, 27), query[0].DateWithFormat);
1544+
Assert.Equal(new DateOnly(2020, 10, 25), query[1].DateWithFormat);
1545+
Assert.Equal(new DateOnly(2020, 6, 1), query[7].DateWithFormat);
1546+
}
15271547
}

0 commit comments

Comments
 (0)