目前,我有:有沒有一種快速的方法將實體轉換爲.csv文件?
string outputRow = string.Empty;
foreach (var entityObject in entityObjects)
{
outputRow = entityObject.field1 + "," + entityObject.Field2 etc....
}
我還是新的實體框架,有沒有更快的方法?
目前,我有:有沒有一種快速的方法將實體轉換爲.csv文件?
string outputRow = string.Empty;
foreach (var entityObject in entityObjects)
{
outputRow = entityObject.field1 + "," + entityObject.Field2 etc....
}
我還是新的實體框架,有沒有更快的方法?
示例代碼,顯示完成你想要的東西,沒有必要硬編碼的屬性名稱(使用反射)的一個簡單而強大的方式:
/// <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();
}
更多內容:
How can i convert a list of objects to csv
Are there any CSV readers/writer lib’s in c#
我把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();
}
}
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";
}
它看起來像所有答案都使用反射。我會好奇的任何方式來做到這一點沒有反思?我猜這可能不可能。 – 2013-01-21 19:36:39
你可以試試我的重量很輕的分隔文件作者:https://gist.github.com/eranbetzalel/5371817#file-delimitedfilewriter-cs – 2013-04-12 13:19:51