Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

enum description support added #603

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion EPPlus/Compatibility/TypeCompat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,29 @@
* ******************************************************************************
* Jan Källman Added 2017-11-02
*******************************************************************************/
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace OfficeOpenXml.Compatibility
{
internal class TypeCompat
{
public static bool IsEnum(Type t)
{
if (t.IsEnum)
{
return true;
}

if (Nullable.GetUnderlyingType(t) != null)
{
return Nullable.GetUnderlyingType(t).IsEnum;
}

return false;
}

public static bool IsPrimitive(object v)
{
#if (Core)
Expand Down
39 changes: 34 additions & 5 deletions EPPlus/ExcelRangeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
using w = System.Windows;
using OfficeOpenXml.Utils;
using OfficeOpenXml.Compatibility;
using EPPlus.Utils;

namespace OfficeOpenXml
{
Expand Down Expand Up @@ -1089,11 +1090,11 @@ internal static string FormatValue(object v, ExcelNumberFormatXml.ExcelFormatTra

private static string GetDateText(DateTime d, string format, ExcelNumberFormatXml.ExcelFormatTranslator nf)
{
if(nf.SpecialDateFormat==ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongDate)
if (nf.SpecialDateFormat == ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongDate)
{
return d.ToLongDateString();
}
else if(nf.SpecialDateFormat == ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongTime)
else if (nf.SpecialDateFormat == ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongTime)
{
return d.ToLongTimeString();
}
Expand Down Expand Up @@ -2099,15 +2100,43 @@ public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool Prin
}
else if (t is PropertyInfo)
{
values[row, col++] = ((PropertyInfo)t).GetValue(item, null);
var property = t as PropertyInfo;
var propertyValue = property.GetValue(item, null);
if (TypeCompat.IsEnum(property.PropertyType))
{
values[row, col++] = EnumHelper.GetDescriptionValue(property.PropertyType, propertyValue);
}
else
{
values[row, col++] = propertyValue;
}
}
else if (t is FieldInfo)
{
values[row, col++] = ((FieldInfo)t).GetValue(item);
var field = t as FieldInfo;
var fieldValue = field.GetValue(item);

if (TypeCompat.IsEnum(field.FieldType))
{
values[row, col++] = EnumHelper.GetDescriptionValue(field.FieldType, fieldValue);
}
else
{
values[row, col++] = field.GetValue(item);
}
}
else if (t is MethodInfo)
{
values[row, col++] = ((MethodInfo)t).Invoke(item, null);
var method = t as MethodInfo;
var methodReturnValue = method.Invoke(item, null);
if (TypeCompat.IsEnum(method.ReturnType))
{
values[row, col++] = EnumHelper.GetDescriptionValue(method.ReturnType, methodReturnValue);
}
else
{
values[row, col++] = ((MethodInfo)t).Invoke(item, null);
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions EPPlus/Utils/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.ComponentModel;

namespace EPPlus.Utils
{
internal static class EnumHelper
{
public static string GetDescriptionValue(Type enumType, object enumValue)
{
if (enumValue != null)
{
if (Nullable.GetUnderlyingType(enumType) != null)
{
enumType = Nullable.GetUnderlyingType(enumType);
}

var fieldInfo = enumType.GetField(enumValue.ToString());

var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false) as DescriptionAttribute[];

if (descriptionAttributes == null) return enumValue.ToString();
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Description : enumValue.ToString();
}

return null;
}
}

}
9 changes: 8 additions & 1 deletion EPPlusTest/TestDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TestDTO
public TestDTO dto { get; set; }
public DateTime Date { get; set; }
public bool Boolean { get; set; }

public TestEnum? Enum { get; set; }
public string GetNameID()
{
return Id + "," + Name;
Expand All @@ -27,4 +27,11 @@ public class InheritTestDTO : TestDTO
public string InheritedProp { get; set; }
}

public enum TestEnum
{
[Description("Item I")]
Item1,

Item2
}
}
Loading