2010-07-07 124 views
9

目前,我有:有没有一种快速的方法将实体转换为.csv文件?

 string outputRow = string.Empty; 
     foreach (var entityObject in entityObjects) 
     { 
      outputRow = entityObject.field1 + "," + entityObject.Field2 etc.... 
     } 

我还是新的实体框架,有没有更快的方法?

+0

它看起来像所有答案都使用反射。我会好奇的任何方式来做到这一点没有反思?我猜这可能不可能。 – 2013-01-21 19:36:39

+0

你可以试试我的重量很轻的分隔文件作者:https://gist.github.com/eranbetzalel/5371817#file-delimitedfilewriter-cs – 2013-04-12 13:19:51

回答

22

示例代码,显示完成你想要的东西,没有必要硬编码的属性名称(使用反射)的一个简单而强大的方式:

/// <summary> 
/// Creates a comma delimeted string of all the objects property values names. 
/// </summary> 
/// <param name="obj">object.</param> 
/// <returns>string.</returns> 
public static string ObjectToCsvData(object obj) 
{ 
    if (obj == null) 
    { 
     throw new ArgumentNullException("obj", "Value can not be null or Nothing!"); 
    } 

    StringBuilder sb = new StringBuilder(); 
    Type t = obj.GetType(); 
    PropertyInfo[] pi = t.GetProperties(); 

    for (int index = 0; index < pi.Length; index++) 
    { 
     sb.Append(pi[index].GetValue(obj, null)); 

     if (index < pi.Length - 1) 
     { 
      sb.Append(","); 
     } 
    } 

    return sb.ToString(); 
} 

更多内容:

Objects to CSV

How can i convert a list of objects to csv

Are there any CSV readers/writer lib’s in c#

Writing a CSV file in .net

LINQ to CSV : Getting data the way you want

LINQ to CSV library

4

我把Leniel的建议,并在一个全功能的“作家”也可以过滤你想要写的属性包裹起来。下面是你使用的代码:

public class CsvFileWriter 
{ 
    public static void WriteToFile<T>(string filePath, List<T> objs, string[] propertyNames) 
    { 
     var builder = new StringBuilder(); 
     var propertyInfos = RelevantPropertyInfos<T>(propertyNames); 
     foreach (var obj in objs) 
      builder.AppendLine(CsvDataFor(obj, propertyInfos)); 

     File.WriteAllText(filePath, builder.ToString()); 
    } 

    public static void WriteToFileSingleFieldOneLine<T>(string filePath, List<T> objs, string propertyName) 
    { 
     var builder = new StringBuilder(); 
     var propertyInfos = RelevantPropertyInfos<T>(new[] { propertyName }); 
     for (var i = 0; i < objs.Count; i++) 
     { 
      builder.Append(CsvDataFor(objs[i], propertyInfos)); 

      if (i < objs.Count - 1) 
       builder.Append(","); 
     } 

     File.WriteAllText(filePath, builder.ToString()); 
    } 

    private static List<PropertyInfo> RelevantPropertyInfos<T>(IEnumerable<string> propertyNames) 
    { 
     var propertyInfos = typeof(T).GetProperties().Where(p => propertyNames.Contains(p.Name)).ToDictionary(pi => pi.Name, pi => pi); 
     return (from propertyName in propertyNames where propertyInfos.ContainsKey(propertyName) select propertyInfos[propertyName]).ToList(); 
    } 

    private static string CsvDataFor(object obj, IList<PropertyInfo> propertyInfos) 
    { 
     if (obj == null) 
      return ""; 

     var builder = new StringBuilder(); 

     for (var i = 0; i < propertyInfos.Count; i++) 
     { 
      builder.Append(propertyInfos[i].GetValue(obj, null)); 

      if (i < propertyInfos.Count - 1) 
       builder.Append(","); 
     } 

     return builder.ToString(); 
    } 
} 
0
string csv = ""; 
//get property names from the first object using reflection  
IEnumerable<PropertyInfo> props = entityObjects.First().GetType().GetProperties(); 

//header 
csv += String.Join(", ",props.Select(prop => prop.Name)) + "\r\n"; 

//rows 
foreach(var entityObject in entityObjects) 
{ 
    csv += String.Join(", ", props.Select(
     prop => (prop.GetValue(entityObject, null) ?? "").ToString() 
    )) 
    + "\r\n"; 
} 
  • 将是更好地使用StringBuilder为许多实体来说的
  • 的代码不检查时entityObjects是空
相关问题