Browse Source

Writing more than a string

Piotr Czajkowski 3 months ago
parent
commit
2fd5b582c2

+ 1 - 1
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net7.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>1.0.2</Version>
+        <Version>1.0.3</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>

+ 4 - 1
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -54,7 +54,10 @@ public class ExcelWriter
             var properties = typeof(T).GetProperties();
             foreach (var property in properties)
             {
-                worksheet.Cell(rowIndex, cellIndex).Value = property.GetValue(value) as string;
+                var valueToSet = property.GetValue(value);
+                if (valueToSet == null) continue;
+                
+                worksheet.Cell(rowIndex, cellIndex).SetCellValue(property, valueToSet);
                 cellIndex++;
             }
 

+ 15 - 0
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -20,4 +20,19 @@ public static class TypeExtensions
         if (valueToSet != null)
             property.SetValue(currentObject, valueToSet); 
     }
+
+    public static void SetCellValue(this IXLCell cell, PropertyInfo property, object? valueToSet)
+    {
+        if (valueToSet == null) return;
+        
+        cell.Value = property.PropertyType switch
+        {
+            not null when property.PropertyType == typeof(string) => valueToSet as string,
+            not null when property.PropertyType == typeof(DateTime?) => valueToSet as DateTime?,
+            not null when property.PropertyType == typeof(TimeSpan?) => valueToSet as TimeSpan?,
+            not null when property.PropertyType == typeof(double?) => valueToSet as double?,
+            not null when property.PropertyType == typeof(int?) => valueToSet as int?,
+            _ => throw new NotSupportedException($"{property.PropertyType} isn't supported!")
+        };
+    }
 }

+ 34 - 0
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -59,4 +59,38 @@ public class WriterTests
         Assert.Equal(5, reader.Read<Test>().Count());
         File.Delete(testFile);
     }
+    
+    [Fact]
+    public void WriteDifferentTypes()
+    {
+        var testFile = Path.GetRandomFileName();
+        testFile = Path.ChangeExtension(testFile, "xlsx");
+
+        var expected = new TestTypes
+        {
+            Date = DateTime.Now,
+            TimeSpan = TimeSpan.MaxValue,
+            Double = 2.33,
+            Int = 1024,
+            Text = "Test"
+        };
+        
+        var list = new List<TestTypes>{ expected };
+        
+        var writer = new ExcelWriter(testFile);
+        writer.Write(list);
+        writer.SaveAs(testFile);
+
+        var reader = new ExcelReader(testFile);
+        var result = reader.Read<TestTypes>().ToList();
+        Assert.Single(result);
+        var first = result.First();
+        Assert.Equal(expected.Date.ToString(), first.Date.ToString());
+        Assert.Equal(expected.TimeSpan, first.TimeSpan);
+        Assert.Equal(expected.Double, first.Double);
+        Assert.Equal(expected.Int, first.Int);
+        Assert.Equal(expected.Text, first.Text);
+
+        File.Delete(testFile);
+    }
 }