Skip to content

Commit

Permalink
#1058, #1042 - added support for System.ComponentModel.DataAnnotation…
Browse files Browse the repository at this point in the history
…s.DisplayAttribute in LoadFromCollection (#1075)
  • Loading branch information
swmal committed Sep 27, 2023
1 parent 5b6b79c commit 54c7714
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
17 changes: 17 additions & 0 deletions src/EPPlus/EPPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream">
<Version>2.2.1</Version>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Drawing.Primitives">
<Version>4.3.0</Version>
</PackageReference>
Expand All @@ -495,6 +498,9 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream">
<Version>2.2.1</Version>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Drawing.Primitives">
<Version>4.3.0</Version>
</PackageReference>
Expand All @@ -507,6 +513,9 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream">
<Version>2.2.1</Version>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.3" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
</ItemGroup>
Expand All @@ -516,6 +525,9 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream">
<Version>2.2.1</Version>
</PackageReference>
<PackageReference Include="System.ComponentModel.Annotations">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="7.0.2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>
Expand Down Expand Up @@ -564,4 +576,9 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="System.ComponentModel.Annotations">
<Version>5.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
55 changes: 35 additions & 20 deletions src/EPPlus/LoadFunctions/LoadFromCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
using System.Text.RegularExpressions;
using OfficeOpenXml.Attributes;
using OfficeOpenXml.Utils;
#if !NET35
using System.ComponentModel.DataAnnotations;
#endif

namespace OfficeOpenXml.LoadFunctions
{
Expand Down Expand Up @@ -331,31 +334,20 @@ private void SetHeaders(object[,] values, Dictionary<int, string> columnFormats,
columnFormats.Add(col, epplusColumnAttribute.NumberFormat);
}
}
else if(!useExistingHeader)
else if (!useExistingHeader)
{
var descriptionAttribute = member.GetFirstAttributeOfType<DescriptionAttribute>();
if (descriptionAttribute != null)
var dotNetHeader = GetHeaderFromDotNetAttributes(member);
if (!string.IsNullOrEmpty(dotNetHeader))
{
header = dotNetHeader;
}
else if(!string.IsNullOrEmpty(colInfo.Header) && colInfo.Header != member.Name)
{
header = descriptionAttribute.Description;
header = colInfo.Header;
}
else
{
var displayNameAttribute = member.GetFirstAttributeOfType<DisplayNameAttribute>();
if (displayNameAttribute != null)
{
header = displayNameAttribute.DisplayName;
}
else
{
if(!string.IsNullOrEmpty(colInfo.Header) && colInfo.Header != member.Name)
{
header = colInfo.Header;
}
else
{
header = ParseHeader(member.Name);
}
}
header = ParseHeader(member.Name);
}
}
}
Expand All @@ -371,6 +363,29 @@ private void SetHeaders(object[,] values, Dictionary<int, string> columnFormats,
row++;
}

private string GetHeaderFromDotNetAttributes(MemberInfo member)
{
var descriptionAttribute = member.GetFirstAttributeOfType<DescriptionAttribute>();
if (descriptionAttribute != null)
{
return descriptionAttribute.Description;
}
var displayNameAttribute = member.GetFirstAttributeOfType<DisplayNameAttribute>();
if (displayNameAttribute != null)
{
return displayNameAttribute.DisplayName;
}
#if !NET35
var displayAttribute = member.GetFirstAttributeOfType<DisplayAttribute>();
if (displayAttribute != null)
{
return displayAttribute.Name;
}
#endif
return default;
}


private string ParseHeader(string header)
{
switch(_headerParsingType)
Expand Down
24 changes: 24 additions & 0 deletions src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ internal class BClass
public int Number { get; set; }
}

internal class CClass
{
[DisplayName("Another property")]
public string AnotherProperty { get; set; }
}

internal class CamelCasedClass
{
public string IdOfThisInstance { get; set; }
Expand Down Expand Up @@ -269,6 +275,24 @@ public void ShouldUseDescriptionAttribute()
}
}

#if !NET35
[TestMethod]
public void ShouldUseDisplayAttribute()
{
var items = new List<CClass>()
{
new CClass(){ AnotherProperty = "asdjfklö "}
};
using (var pck = new ExcelPackage(new MemoryStream()))
{
var sheet = pck.Workbook.Worksheets.Add("sheet");
sheet.Cells["C1"].LoadFromCollection(items, true, TableStyles.Dark1);

Assert.AreEqual("Another property", sheet.Cells["C1"].Value);
}
}
#endif

[TestMethod]
public void ShouldUseBaseClassProperties()
{
Expand Down

0 comments on commit 54c7714

Please sign in to comment.