EPPlus
EPPlus copied to clipboard
Support for Dictionaries with attributes in LoadFromCollection
Via the attributes we could define a set of keys that should/could be present in a dictionary on each object in the list. Each key will become a column in the order they are defined.
Could look something like this:
[EPPlusTableColumn(Order=3, Keys=new string[]{ "Foo", "Bar" })]
public Dictionary<string, int> Quarters { get; set; }
Complex types as the value of the dictionary could be supported following the logic of nested classes.
So what you showed would not work as that's compile time. The set of keys for the Quarters dictionary would be determined at runtime, and is stored in a HashSet<string>.
Here's a better example of what I'm doing:
var headers = new List<string> { "Barcode", "Current Org Level 3", "Current Org Level 4", "Current Org Level 5" };
headers.AddRange(data.Dates); // <=- This is a sorted list of date strings that are dynamically generated.
for (var i = 0; i < headers.Count; i++) {
var column = i + 1;
ws.Cells[1, column].Value = headers[i];
ws.Cells[1, column].Style.Font.Bold = true;
}
var row = 2;
foreach (var (barcode, value) in data.Entries) {
var column = 1;
var range = ws.Cells[row, column++];
range.Value = barcode;
range.Style.Font.Bold = true;
ws.Cells[row, column++].Value = value.ThreeName;
ws.Cells[row, column++].Value = value.FourName;
ws.Cells[row, column++].Value = value.FiveName;
// This is a Dictionary<string, int> where keys will be from the date list above.
// 👇
foreach (var date in data.Dates) {
try {
if (value.Values.TryGetValue(date, out var result) is false)
continue;
ws.Cells[row, column].Value = result;
} finally {
column++;
}
}
row++;
}
Thanks, let's see if I understand this correctly:
- We need a new type of attribute that should have a property/member that defines the headers in the order they should appear as columns in the spreadsheet (corresponding to your headers variable above).
- This attribute should only be used on properties that are of the Dictionary<string,*> type. If used on another type of member an exception will be thrown when LoadFromCollection is called.
- When loading the data into the range we use the value returned by each key if present in the Dictionary.
- The result will be that cells that corresponds to a key that wasn't present in the Dictionary will be empty or contain the value returned if the key was present in the Dictionary.
Exactly.
From: Mats Alm @.> Sent: Tuesday, August 8, 2023 7:00 AM To: EPPlusSoftware/EPPlus @.> Cc: Grosch, Scott @.>; Manual @.> Subject: Re: [EPPlusSoftware/EPPlus] Support for Dictionaries with attributes in LoadFromCollection (Issue #969)
Thanks, let's see if I understand this correctly:
- We need a new type of attribute that should have a property/member that defines the headers in the order they should appear as columns in the spreadsheet (corresponding to your headers variable above).
- This attribute should only be used on properties that are of the Dictionary<string,*> type. If used on another type of member an exception will be thrown when LoadFromCollection is called.
- When loading the data into the range we use the value returned by each key if present in the Dictionary.
- The result will be that cells that corresponds to a key that wasn't present in the Dictionary will be empty or contain the value returned if the key was present in the Dictionary.
— Reply to this email directly, view it on GitHubhttps://github.com/EPPlusSoftware/EPPlus/issues/969#issuecomment-1669678023, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AUN5BJGGIVCTO4SZ5VU4W2TXUJA67ANCNFSM6AAAAAA2KJKDCE. You are receiving this because you are subscribed to this thread.Message ID: @.@.>>