Tuesday, January 12, 2010

Generate a CSV from a generic list of objects using reflection and extension methods

http://www.joe-stevens.com/2009/08/03/generate-a-csv-from-a-generic-list-of-objects-using-reflection-and-extension-methods/

using System.Text;
using System.Reflection;

public static class Extensions
{
public static string GetCSV(this List list)
{
StringBuilder sb = new StringBuilder();

//Get the properties for type T for the headers
PropertyInfo[] propInfos = typeof(T).GetProperties();
for (int i = 0; i <= propInfos.Length - 1; i++)
{
sb.Append(propInfos[i].Name);

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

sb.AppendLine();

//Loop through the collection, then the properties and add the values
for (int i = 0; i <= list.Count - 1; i++)
{
T item = list[i];
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(",");
}
}

sb.AppendLine();
}

return sb.ToString();
}
}

No comments: