2012-04-12 45 views
0

我正在研究將導入嵌套屬性(對象)的對象導出到csv文件。導出帶有嵌套屬性和值的對象csv

這裏的代碼:

/// <summary> 
    /// Exports a CSV 
    /// </summary> 
    /// <param name="csv">The CSV data as a string</param> 
    /// <param name="filename">The filename for the exported file</param> 
    public static void ExportCSV(string csv, string filename) 
    { 
     StreamWriter writer = new StreamWriter(filename); 
     try 
     { 
      writer.Write(csv); 
     } 
     catch (FileNotFoundException ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      writer.Close(); 
     } 
    } 

    /// <summary> 
    /// Generate the CSV data as a string using reflection on the objects in the list 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="list">The generic list</param> 
    /// <returns></returns> 
    public static string GetCSV<T>(this List<T> list) 
    { 
     StringBuilder sb = new StringBuilder(); 

     for (int i = 0; i < list.Count; i++) 
     { 
      sb.AppendLine(GetPropertiesString<T>(list[i])); 
     } 

     return sb.ToString(); 
    } 


    public static string GetPropertiesString<T>(T item) 
    { 
     StringBuilder sb = new StringBuilder(); 
     PropertyInfo[] propInfos = typeof(T).GetProperties(); 

     for (int i = 0; i <= propInfos.Length - 1; i++) 
     { 
      Type propertyType = propInfos[i].PropertyType; 

      if (propertyType != typeof(string) && propertyType != typeof(int) && propertyType != typeof(double) && propertyType != typeof(float) && propertyType != typeof(decimal)) 
      { 
       string test = GetPropertiesString(propertyType); 
      } 

      sb.Append(propInfos[i].Name); 

      if (i < propInfos.Length - 1) 
      { 
       sb.Append(","); 
      } 
     } 

     sb.AppendLine(); 

     for (int j = 0; j <= propInfos.Length - 1; j++) 
     { 
      object o = item.GetType().GetProperty(propInfos[j].Name).GetValue(item, null); 

      if (o != null) 
      { 
       string value = o.ToString(); 

       //Check if the value contans a comma and place it in quotes if so 
       if (value.Contains(",")) 
       { 
        value = string.Concat("\"", value, "\""); 
       } 

       //Replace any \r or \n special characters from a new line with a space 
       if (value.Contains("\r")) 
       { 
        value = value.Replace("\r", " "); 
       } 
       if (value.Contains("\n")) 
       { 
        value = value.Replace("\n", " "); 
       } 

       sb.Append(value); 
      } 

      if (j < propInfos.Length - 1) 
      { 
       sb.Append(","); 
      } 
     } 

     return sb.ToString(); 
    } 
} 

}

我在這裏的目的

/// <summary> 
/// Basic Person object 
/// </summary> 
public class Person 
{ 
    public string Forename { get; set; } 
    public string Surname { get; set; } 
    public int Age { get; set; } 
    public Adress Adress { get; set; } 
} 

public class Adress 
{ 
    public string Place { get; set; } 
    public int PLZ { get; set; } 
} 

所需的CSV文件格式:

header: Forename, Surename, Age, Place, PLZ 
values: Joe, Stevens, 30,Town1, 11111 

我試圖調用該方法遞歸,但後來呢PropertyInfo [] propInfos = typeof(T).GetProperties()的結果有奇怪的項目?

應該可以將對象與任何嵌套對象深入導出。

有什麼建議?

感謝, 托爾斯滕

回答

0

按照預期的代碼不工作,因爲你不正確的類型調用方法遞歸。

GetPropertiesString(propertyType); 

應該叫這樣的:

dynamic ob = Activator.CreateInstance(propertyType); 
string test = GetPropertiesString(ob); 
+0

你好,謝謝你。我試過你沒有成功的例子。我如何檢查屬性是否是嵌套對象。此刻我用一個簡單的if(propertyType!= typeof(string))來檢查它,但我認爲這不正確或不方便。 – tro 2012-04-12 08:49:49

+0

問題是你用System.Type調用GetPropertiesString,這就是爲什麼你正在獲取其他屬性。因此,用我的代碼替換,並嘗試與調試器一起步,看看你會得到嵌套的類型屬性。你的代碼有幾個問題,因爲當你遞歸地調用方法時,你重新實例化了你的StringBuilder,所以你放棄了以前的結果 – ionden 2012-04-12 08:54:26