Skip to content
Merged
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
27 changes: 24 additions & 3 deletions src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,35 @@ namespace OfficeOpenXml.Attributes
public abstract class EpplusTableColumnAttributeBase : Attribute
{

private int _order = int.MaxValue;
private bool _orderIsSet = false;

/// <summary>
/// Order of the columns value, default value is 0
/// </summary>
public int Order
{
get;
set;
} = int.MaxValue;
get
{
return _order;
}
set
{
_order = value;
_orderIsSet = true;
}
}

/// <summary>
/// Returns true if the Order property has been explicitly set.
/// </summary>
internal bool OrderIsSet
{
get
{
return _orderIsSet;
}
}

/// <summary>
/// Name shown in the header row, overriding the property name
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus/LoadFunctions/LoadFromCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private string GetHeaderFromDotNetAttributes(MemberInfo member)
var displayAttribute = member.GetFirstAttributeOfType<DisplayAttribute>();
if (displayAttribute != null)
{
return displayAttribute.Name;
return displayAttribute.GetName();
}
#endif
return default;
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus/LoadFunctions/ReflectionHelpers/MemberPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override string GetHeader()
#if !NET35
else if (last.Member.HasAttributeOfType(out DisplayAttribute displayAttr))
{
header = displayAttr.Name;
header = displayAttr.GetName();
}
#endif
if (string.IsNullOrEmpty(header))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static int GetSortOrder(this MemberInfo member, List<MemberInfo> filterMe
{
sortOrder = entcAttr.Order;
}
else if(member.HasAttributeOfType(out EpplusTableColumnAttribute etcAttr))
else if (member.HasAttributeOfType(out EpplusTableColumnAttribute etcAttr) && etcAttr.OrderIsSet)
{
sortOrder = etcAttr.Order;
}
Expand Down
11 changes: 11 additions & 0 deletions src/EPPlusTest/EPPlus.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<Compile Update="Filter\ValueFilterTest.cs" />
<Compile Update="Filter\DynamicFilterTest.cs" />
<Compile Update="Filter\AutoFilterReadWriteTest.cs" />
<Compile Update="LoadFunctions\AttributesTestClasses\LoadFromCollResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>LoadFromCollResources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="App.config">
Expand Down Expand Up @@ -272,6 +277,12 @@
<ItemGroup>
<Folder Include="Style\XmlAccess\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="LoadFunctions\AttributesTestClasses\LoadFromCollResources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>LoadFromCollResources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>
<SsdtUnitTestVersion>3.1</SsdtUnitTestVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class where only DisplayAttribute is present (no EpplusTableColumnAttribute).
/// DisplayAttribute.Order should be used in this case.
/// </summary>
public class ClassWithDisplayOrderOnly
{
[Display(Name = "Id Column", Order = 3)]
public int Id { get; set; }

[Display(Name = "Name Column", Order = 1)]
public string Name { get; set; }

[Display(Name = "Description Column", Order = 2)]
public string Description { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
using OfficeOpenXml.Attributes;
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class using DisplayAttribute with ResourceType.
/// When ResourceType is set, GetName() should be used instead of Name
/// to get the localized value from the resource class.
/// </summary>
public class ClassWithDisplayResourceType
{
[EpplusTableColumn(Order = 1)]
[Display(Name = "IdHeader", ResourceType = typeof(LoadFromCollResources))]
public int Id { get; set; }

[EpplusTableColumn(Order = 2)]
[Display(Name = "NameHeader", ResourceType = typeof(LoadFromCollResources))]
public string Name { get; set; }

[EpplusTableColumn(Order = 3)]
[Display(Name = "DescriptionHeader", ResourceType = typeof(LoadFromCollResources))]
public string Description { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class using DisplayAttribute with ResourceType but without EpplusTableColumnAttribute.
/// GetName() should return the localized value from the resource class.
/// </summary>
public class ClassWithDisplayResourceTypeOnly
{
[Display(Name = "IdHeader", ResourceType = typeof(LoadFromCollResources), Order = 1)]
public int Id { get; set; }

[Display(Name = "NameHeader", ResourceType = typeof(LoadFromCollResources), Order = 2)]
public string Name { get; set; }

[Display(Name = "DescriptionHeader", ResourceType = typeof(LoadFromCollResources), Order = 3)]
public string Description { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
using OfficeOpenXml.Attributes;
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class where EpplusTableColumnAttribute.Header IS set.
/// It should take precedence over DisplayAttribute.Name.
/// </summary>
public class ClassWithEpplusHeaderAndDisplayName
{
[EpplusTableColumn(Order = 1, Header = "EPPlus Id")]
[Display(Name = "Display Id")]
public int Id { get; set; }

[EpplusTableColumn(Order = 2, Header = "EPPlus Name")]
[Display(Name = "Display Name")]
public string Name { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using OfficeOpenXml.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class where EpplusTableColumnAttribute exists but Order is NOT set.
/// Should fall back to DisplayAttribute.Order.
/// </summary>
public class ClassWithEpplusNoOrderAndDisplayWithOrder
{
[EpplusTableColumn(NumberFormat = "0")]
[Display(Name = "Id Column", Order = 3)]
public int Id { get; set; }

[EpplusTableColumn]
[Display(Name = "Name Column", Order = 1)]
public string Name { get; set; }

[EpplusTableColumn]
[Display(Name = "Description Column", Order = 2)]
public string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
using OfficeOpenXml.Attributes;
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class where EpplusTableColumnAttribute has Order set but NOT Header.
/// Header should fall back to DisplayAttribute.GetName().
/// </summary>
public class ClassWithEpplusOrderAndDisplayName
{
[EpplusTableColumn(Order = 3)]
[Display(Name = "The Id")]
public int Id { get; set; }

[EpplusTableColumn(Order = 1)]
[Display(Name = "The Name")]
public string Name { get; set; }

[EpplusTableColumn(Order = 2)]
[Display(Name = "The Description")]
public string Description { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
using OfficeOpenXml.Attributes;
#if !NET35
using System.ComponentModel.DataAnnotations;

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
/// <summary>
/// Test class where EpplusTableColumnAttribute has Order set but
/// DisplayAttribute does NOT have Order set.
/// EpplusTableColumnAttribute.Order should be used.
/// </summary>
public class ClassWithEpplusOrderAndDisplayWithoutOrder
{
[EpplusTableColumn(Order = 3)]
[Display(Name = "Id Column")]
public int Id { get; set; }

[EpplusTableColumn(Order = 1)]
[Display(Name = "Name Column")]
public string Name { get; set; }

[EpplusTableColumn(Order = 2)]
[Display(Name = "Description Column")]
public string Description { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************/
using OfficeOpenXml.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !NET35
using System.ComponentModel.DataAnnotations;
#endif

namespace EPPlusTest.LoadFunctions.AttributesTestClasses
{
#if !NET35
/// <summary>
/// Test class where EpplusTableColumnAttribute.Order should take precedence
/// over DisplayAttribute.Order when both are present on the same property.
/// </summary>
public class ClassWithEpplusTableColumnAndDisplayOrder
{
// EpplusTableColumn Order = 3, Display Order = 1
// Expected column order should be 3 (EpplusTableColumn wins)
[EpplusTableColumn(Order = 3)]
[Display(Name = "Id Column", Order = 1)]
public int Id { get; set; }

// EpplusTableColumn Order = 1, Display Order = 3
// Expected column order should be 1 (EpplusTableColumn wins)
[EpplusTableColumn(Order = 1)]
[Display(Name = "Name Column", Order = 3)]
public string Name { get; set; }

// EpplusTableColumn Order = 2, Display Order = 2
[EpplusTableColumn(Order = 2)]
[Display(Name = "Description Column", Order = 2)]
public string Description { get; set; }
}
#endif
}
Loading
Loading