I am developing a class that gets list of objects and export it to excel.
I am using EPPlus and generics. Basically the solution is to:
- Create a generic class that gets list of objects
- Generate the header using GetProperties
- Generate the data
My problem is to get the localized string for the header property
I have a class defined as follow:
public class OrderToExport
{
[Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderID")]
public int OrderID { get; set; }
public string UserName { get; set; }
// Shipping Information
[Display(ResourceType = typeof(OrdersManagementStrings), Name = "ShippingType")]
public Order.eShippingType ShippingType { get; set; }
[Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderDate")]
public DateTime OrderDate { get; set; }
public string InstituteName { get; set; }
public decimal TotalPrice { get; set; }
[Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderType")]
public Order.eOrderType EOrderType { get; set; }
public string ShipingAddress { get; set; }
[Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderStatus")]
public Order.eOrderStatus EOrderStatus { get; set; }
}
I have a Method:
public ExportToExcel(List<T> i_obj, string i_SheetName)
{
foreach (var prop in typeof(T).GetProperties())
{
//var n = prop.GetCustomAttributes(true).OfType<T>().Cast<T>();
ws.Cells[row, column].Value = GetDisplayName(prop);
column++;
}
}
private string GetDisplayName(PropertyInfo property)
{
var attrName = GetAttributeDisplayName(property);
if (!string.IsNullOrEmpty(attrName))
return attrName;
var metaName = GetMetaDisplayName(property);
if (!string.IsNullOrEmpty(metaName))
return metaName;
return property.Name.ToString();
}
private string GetAttributeDisplayName(PropertyInfo property)
{
var atts = property.GetCustomAttributes(true);
if (atts.Length == 0)
return null;
return (atts[0] as DisplayNameAttribute).DisplayName;
}
private string GetMetaDisplayName(PropertyInfo property)
{
var atts = property.DeclaringType.GetCustomAttributes(true);
if (atts.Length == 0)
return null;
var metaAttr = atts[0] as MetadataTypeAttribute;
var metaProperty =
metaAttr.MetadataClassType.GetProperty(property.Name);
if (metaProperty == null)
return null;
return GetAttributeDisplayName(metaProperty);
}
Now, I don't know how to get the sting stored in the resource manager (OrdersManagementStrings) for that property for example the localized property for OrderID
.
Any help will be appreciated