Skip to content

Commit

Permalink
Added support for Uri mapping (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelebastione authored Feb 19, 2025
1 parent 9b37e22 commit b7f7816
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Binary file added samples/xlsx/TestUriMapping.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions src/MiniExcel/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
else
newValue = Enum.Parse(pInfo.ExcludeNullableType, itemValue?.ToString(), true);
}
else if (pInfo.ExcludeNullableType == typeof(Uri))
{
var rawValue = itemValue?.ToString();
if (!Uri.TryCreate(rawValue, UriKind.RelativeOrAbsolute, out var uri))
throw new InvalidCastException($"Value \"{rawValue}\" cannot be converted to Uri");
newValue = uri;
}
else
{
// Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138
Expand Down
21 changes: 21 additions & 0 deletions tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,27 @@ public void AutoCheckTypeTest()
}
}

public class ExcelUriDemo
{
public string Name { get; set; }
public int Age { get; set; }
public Uri Url { get; set; }
}

[Fact]
public void UriMappingTest()
{
var path = "../../../../../samples/xlsx/TestUriMapping.xlsx";
using (var stream = File.OpenRead(path))
{
var rows = stream.Query<ExcelUriDemo>().ToList();

Assert.Equal("Felix", rows[1].Name);
Assert.Equal(44, rows[1].Age);
Assert.Equal(new Uri("https://friendly-utilization.net"), rows[1].Url);
}
}

[Fact()]
public void TestDatetimeSpanFormat_ClosedXml()
{
Expand Down

0 comments on commit b7f7816

Please sign in to comment.