Hi all,
I wrote a method which exports values to excel file from an IEnumerable parameter.
Method worked fine for my little test class and i was happy till i test my method with
a LINQ class.
Method:
public static void IEnumerableToExcel<T>(IEnumerable<T> data, HttpResponse Response)
{
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Charset = "windows-1254";
// set MIME type to be Excel file.
Response.ContentType = "application/vnd.ms-excel;charset=windows-1254";
// add a header to response to force download (specifying filename)
Response.AddHeader("Content-Disposition", "attachment;filename=\"ResultFile.xls\"");
Type typeOfT = typeof(T);
List<string> result = new List<string>();
FieldInfo[] fields = typeOfT.GetFields();
foreach (FieldInfo info in fields)
{
result.Add(info.Name);
}
Response.Write(String.Join("\t", result.ToArray()) + "\n");
foreach (T t in data)
{
result.Clear();
foreach (FieldInfo f in fields)
result.Add((typeOfT).GetField(f.Name).GetValue(t).ToString());
Response.Write(String.Join("\t", result.ToArray()) + "\n");
}
Response.End();
}
My Little Test Class:
public class Mehmet
{
public string FirstName;
public string LastName;
}
Two Usage:
Success:
Mehmet newMehmet = new Mehmet();
newMehmet.FirstName = "Mehmet";
newMehmet.LastName = "Altiparmak";
List<Mehmet> list = new List<Mehmet>();
list.Add(newMehmet);
DeveloperUtility.Export.IEnumerableToExcel<Mehmet>(list, Response);
Fail:
ExtranetDataContext db = new ExtranetDataContext();
DeveloperUtility.Export.IEnumerableToExcel<User>(db.Users, Response);
Fail Reason: When the code reaches the code block used to get the fields of the template(T), it can not find any field for the LINQ Generated User class. For my weird class it works fine.
Why?
Thanks...