viernes, 18 de julio de 2008

Exportar una coleccion a CSV usando reflection

El siguiente ejemplo exporta una coleccion de objetos que implemente la interfaz IEnumerable a CSV usando reflection. Se eliminaron algunas validaciones e identaciones para que ocupe menos y dejar lo principal.



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.IO;


public class ExportToCSV
{
public ExportToCSV()
{
//
// TODO: Add constructor logic here
//
}

IEnumerable _content = null;
String _filename = String.Empty;

public ExportToCSV(String fileName, IEnumerable content)
{
this._content = content;
this._filename = fileName;
}

public String Filename
{
get { return this._filename; }
set { this._filename = value; }
}

public IEnumerable Content
{
get { return this._content; }
set { this._content = value; }
}


private PropertyInfo FindProperty(String propertyName, PropertyInfo[] properties)
{
PropertyInfo result = null;
foreach (PropertyInfo p in properties)
{
if (String.Compare(propertyName, p.Name, true) == 0)
{
return p;
}
}
return result;
}

private string GetCSVColumns()
{
Object item = GetFirstItem();

if ((item != null))
{
Type itemType = item.GetType();
PropertyInfo[] publicProperties = GetPublicProperties(itemType);
return GetCSVColumnsFromPublicProperties(publicProperties);
}

return String.Empty ;
}

private static PropertyInfo[] GetPublicProperties(Type itemType)
{
PropertyInfo[] allTheProperties = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
List
publicProperties = new List();
foreach (PropertyInfo p in allTheProperties)
{
if (p.GetIndexParameters().Length == 0)
{
publicProperties.Add(p);
}
}

return publicProperties.ToArray();
}


private Object GetFirstItem()
{
Object item = null;
IEnumerator iterador = this._content.GetEnumerator();
if (iterador.MoveNext())
{
item = iterador.Current;
}
return item;
}

private static string GetCSVColumnsFromPublicProperties(PropertyInfo[] publicProperties)
{
StringBuilder sb = new StringBuilder();
String propertyValue = String.Empty;
bool isFirst = true;

foreach (PropertyInfo p in publicProperties)
{
isFirst = AppenCSVSeparator(sb, isFirst);
sb.Append(p.Name);

}

return sb.ToString();
}

private string GetCSVLine(Object item)
{
Type itemType = item.GetType();
PropertyInfo[] publicProperties = GetPublicProperties(itemType);
StringBuilder sb = new StringBuilder();
String propertyValue = String.Empty;
bool isFirst = true;

foreach (PropertyInfo p in publicProperties)
{
if (p.GetIndexParameters().Length == 0)
{
isFirst = AppenCSVSeparator(sb, isFirst);

propertyValue = p.GetValue(item, null).ToString();

sb.Append(propertyValue);
}
}
return sb.ToString();
}


private static bool AppenCSVSeparator(StringBuilder sb, bool isFirst)
{
if (!isFirst)
{
sb.Append(";");
}

isFirst = false;
return isFirst;
}

private void AppendContentToStream(StreamWriter writer)
{
IEnumerator iterador = this.Content.GetEnumerator();
Object lastSelected = null;
while (iterador.MoveNext())
{
Object item = iterador.Current;
writer.WriteLine(this.GetCSVLine(iterador.Current));
lastSelected = iterador.Current;
}
}


private void ValidateHasContent()
{
if (this._content == null) throw new Exception("No hay content");
}

public void FillWithCSV(Stream stream)
{
ValidateHasContent();
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(this.GetCSVColumns());
AppendContentToStream(writer);
writer.Flush();
}
}

No hay comentarios: