Skip to content

Commit b7f7816

Browse files
Added support for Uri mapping (#726)
1 parent 9b37e22 commit b7f7816

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

samples/xlsx/TestUriMapping.xlsx

7.73 KB
Binary file not shown.

src/MiniExcel/Utils/TypeHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
151151
else
152152
newValue = Enum.Parse(pInfo.ExcludeNullableType, itemValue?.ToString(), true);
153153
}
154+
else if (pInfo.ExcludeNullableType == typeof(Uri))
155+
{
156+
var rawValue = itemValue?.ToString();
157+
if (!Uri.TryCreate(rawValue, UriKind.RelativeOrAbsolute, out var uri))
158+
throw new InvalidCastException($"Value \"{rawValue}\" cannot be converted to Uri");
159+
newValue = uri;
160+
}
154161
else
155162
{
156163
// Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138

tests/MiniExcelTests/MiniExcelOpenXmlTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,27 @@ public void AutoCheckTypeTest()
381381
}
382382
}
383383

384+
public class ExcelUriDemo
385+
{
386+
public string Name { get; set; }
387+
public int Age { get; set; }
388+
public Uri Url { get; set; }
389+
}
390+
391+
[Fact]
392+
public void UriMappingTest()
393+
{
394+
var path = "../../../../../samples/xlsx/TestUriMapping.xlsx";
395+
using (var stream = File.OpenRead(path))
396+
{
397+
var rows = stream.Query<ExcelUriDemo>().ToList();
398+
399+
Assert.Equal("Felix", rows[1].Name);
400+
Assert.Equal(44, rows[1].Age);
401+
Assert.Equal(new Uri("https://friendly-utilization.net"), rows[1].Url);
402+
}
403+
}
404+
384405
[Fact()]
385406
public void TestDatetimeSpanFormat_ClosedXml()
386407
{

0 commit comments

Comments
 (0)