EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.8k stars 274 forks source link

Update to 7.1 ExcelRange.LoadFromCollection error: The Order property has not been set. Use the GetOrder method to get the value. #1395

Open TheQuake opened 6 months ago

TheQuake commented 6 months ago

EPPlus usage

Noncommercial use

Environment

Windows IIS

Epplus version

7.1.0

Spreadsheet application

Excel

Description

Update to 7.1 ExcelRangeBase.LoadFromCollection error: The Order property has not been set. Use the GetOrder method to get the value. The changelog specifically mentions an ExcelRangeBase.LoadFromCollection improvment. What needs to be changed?

swmal commented 6 months ago

@TheQuake - thanks for reporting. EPPlus does not throw an Exception with this message from what I can see. It seems to come from the System.ComponentModel.DataAnnotations.Display attribute.

Can you provide some more information that might help us to replicate this issue? The code for the T class that you use in LoadFromCollection<T> would be useful.

TheQuake commented 6 months ago

@swmal - Thanks! It's a POCO that had an attribute over a single read-only (get only) property, specifically: ( [Display(Name = "Product")] ). Since the exception was related to annotations, I removed it & voila, it works now. I don't need that decoration, but if you wish to pursue this further, let me know.

RicPigeon commented 4 months ago

I encountered the same issue and was able to work around it with the following code:

MemberInfo[] listeMembers = (from MemberInfo x in typeof(T).GetProperties() select x).ToArray();
ws.Cells["A1"].LoadFromCollection<T>(list, true, null, BindingFlags.Default, listeMembers);

It seems that the problem occurs with the DisplayAttribute. The Order property must be specified for all class property members, like this:

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

Otherwise, an exception is thrown.

I think the exception is throw here : https://github.com/EPPlusSoftware/EPPlus/blob/1859e7159b3a73037d61b5f05ba8d139d2644c8f/src/EPPlus/LoadFunctions/ReflectionHelpers/SortOrderExtensions.cs#L69 when i refer to DataAnnotations doc : https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/DisplayAttribute.cs#L254