Browse Source

Able to get type

Piotr Czajkowski 2 months ago
parent
commit
2ec47d386d

+ 2 - 0
ExcelORM/ExcelORM/ExcelDynamicReader.cs

@@ -26,6 +26,8 @@ public class ExcelDynamicReader
                 var cell = row.Cell(item.Position);
                 if (cell == null || cell.Value.IsBlank) continue;
 
+                if (item.Type == null) item.Type = cell.Value.ValueType();
+
                 var cellItem = item with
                 {
                     Value = cell.Value.ToObject()

+ 17 - 1
ExcelORM/ExcelORMTests/DynamicReaderTests.cs

@@ -5,7 +5,8 @@ namespace ExcelORMTests;
 public class DynamicReaderTests
 {
     private const string RegularFile = "testFiles/first.xlsx";
-    
+    private const string DifferentTypesFile = "testFiles/differentTypes.xlsx";
+
     [Fact]
     public void Read()
     {
@@ -13,4 +14,19 @@ public class DynamicReaderTests
         var results = reader.Read("Sheet 1").ToArray();
         Assert.NotEmpty(results);
     }
+
+    [Fact]
+    public void ReadDifferentTypes()
+    {
+        var reader = new ExcelDynamicReader(DifferentTypesFile);
+        var results = reader.Read("Sheet1").ToArray();
+        Assert.NotEmpty(results);
+
+        var first = results.First();
+        Assert.Equal(typeof(string), first[0].Type);
+        Assert.Equal(typeof(DateTime?), first[1].Type);
+        Assert.Equal(typeof(TimeSpan?), first[2].Type);
+        Assert.Equal(typeof(double?), first[3].Type);
+        Assert.Equal(typeof(double?), first[4].Type);
+    }
 }