0

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:

  1. Create a generic class that gets list of objects
  2. Generate the header using GetProperties
  3. 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

Silagy
  • 3,053
  • 2
  • 27
  • 39
  • Hope this will help you https://www.devexpress.com/Support/Center/Question/Details/A2820 – Avijit Dec 30 '14 at 21:11
  • @Avijit Unfortunately it doesn't help because i don't know how to get the resource name from the property. without the resource name and the property i can't get the localized string – Silagy Dec 30 '14 at 21:14
  • 1
    OK. Here it is http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx – Avijit Dec 30 '14 at 21:21

2 Answers2

0

Here is the answer geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx

Thanks to @Avijit

Silagy
  • 3,053
  • 2
  • 27
  • 39
0

Have a look here: http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx To learn how to change the culture to use during runtime, see the second comment in the link:

switch (comboBox1.Text)
{
    case "neutral":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("");
        break;
    case "en-GB":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
        break;
    case "de-DE":
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
        break;
}

string messageText = Messages.MsgSampleText;
MessageBox.Show(messageText); 
Avijit
  • 1,219
  • 2
  • 15
  • 28