2 Commits 2fd5b582c2 ... 14bd69e357

Author SHA1 Message Date
  Piotr Czajkowski 14bd69e357 Cleaner/better way of reading values 3 months ago
  Piotr Czajkowski dd7d00aeee Stop reinventing the wheel and RTFM (or code) 3 months ago

+ 2 - 1
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net7.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>1.0.3</Version>
+        <Version>1.0.4</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -14,6 +14,7 @@
 	<Authors>Piotr Czajkowski</Authors>
 	<Description>Simple library to read/write C# objects from/to Excel files. </Description>
 	<RepositoryType>GitHub</RepositoryType>
+	<PackageReleaseNotes>Handling less types, but better.</PackageReleaseNotes>
     </PropertyGroup>
 
     <ItemGroup>

+ 1 - 1
ExcelORM/ExcelORM/ExcelReader.cs

@@ -31,7 +31,7 @@ public class ExcelReader
                 var property = type.GetProperty(item.PropertyName);
                 if (property == null) continue;
 
-                current.SetValue(property, cell);
+                current.SetPropertyValue(property, cell.Value);
             }
 
             yield return current;

+ 1 - 1
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -57,7 +57,7 @@ public class ExcelWriter
                 var valueToSet = property.GetValue(value);
                 if (valueToSet == null) continue;
                 
-                worksheet.Cell(rowIndex, cellIndex).SetCellValue(property, valueToSet);
+                worksheet.Cell(rowIndex, cellIndex).Value = XLCellValue.FromObject(valueToSet);
                 cellIndex++;
             }
 

+ 22 - 21
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -3,36 +3,37 @@ using ClosedXML.Excel;
 
 namespace ExcelORM;
 
+// Borrowed from https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Excel/XLCellValue.cs#L361
 public static class TypeExtensions
 {
-    public static void SetValue<T>(this T currentObject, PropertyInfo property, IXLCell cell)
+    private static object? ToObject(this XLCellValue value)
     {
-        object? valueToSet = property.PropertyType switch
+        return value.Type switch
         {
-            not null when property.PropertyType == typeof(string) => cell.Value.ToString(),
-            not null when property.PropertyType == typeof(DateTime?) => cell.Value.IsDateTime ? cell.Value.GetDateTime() : null,
-            not null when property.PropertyType == typeof(TimeSpan?) => cell.Value.IsTimeSpan ? cell.Value.GetTimeSpan() : null,
-            not null when property.PropertyType == typeof(double?) => cell.Value.IsNumber ? cell.Value.GetNumber() : null,
-            not null when property.PropertyType == typeof(int?) => cell.Value.IsNumber ? (int?)cell.Value.GetNumber() : null,
-            _ => throw new NotSupportedException($"{property.PropertyType} isn't supported!")
+            XLDataType.Blank => null,
+            XLDataType.Boolean => value.GetBoolean(),
+            XLDataType.Number => value.GetNumber(),
+            XLDataType.Text => value.GetText(),
+            XLDataType.Error => value.GetError(),
+            XLDataType.DateTime => value.GetDateTime(),
+            XLDataType.TimeSpan => value.GetTimeSpan(),
+            _ => throw new InvalidCastException()
         };
-               
-        if (valueToSet != null)
-            property.SetValue(currentObject, valueToSet); 
     }
 
-    public static void SetCellValue(this IXLCell cell, PropertyInfo property, object? valueToSet)
+    public static void SetPropertyValue<T>(this T currentObject, PropertyInfo property, XLCellValue value)
     {
+        var valueToSet = value.ToObject();
         if (valueToSet == null) return;
-        
-        cell.Value = property.PropertyType switch
+
+        try
         {
-            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!")
-        };
+            property.SetValue(currentObject, valueToSet);
+        }
+        catch
+        {
+            valueToSet = value.ToString();
+            property.SetValue(currentObject, valueToSet);
+        }
     }
 }

+ 1 - 1
ExcelORM/ExcelORMTests/TestTypes.cs

@@ -5,6 +5,6 @@ public record TestTypes
     public string? Text { get; set; }
     public DateTime? Date { get; set; }
     public TimeSpan? TimeSpan { get; set; }
-    public int? Int { get; set; }
+    public double? Int { get; set; }
     public double? Double { get; set; }
 }