original post : http://www.joe-stevens.com/2009/08/03/generate-a-csv-from-a-generic-list-of-objects-using-reflection-and-extension-methods/
in my Utils.cs class :
public static void ExportCSV<T>(this List<T> list, string filename) { string csv = GetCsv(list); CSVHelper(csv, filename); } private static string GetCsv<T>(this List<T> 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(); } private static void CSVHelper(string csv, string filename) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.csv", filename)); HttpContext.Current.Response.ContentType = "text/csv"; HttpContext.Current.Response.AddHeader("Pragma", "public"); HttpContext.Current.Response.Write(csv); HttpContext.Current.Response.End(); }
in my controller:
public void ExportCSV(string StartDate, string EndDate, string DaysLate, string IncludeFreelancers) { var list = GetDefaultTimeReportList(StartDate, EndDate, DaysLate, IncludeFreelancers); list.ExportCSV(string.Format("TimeReport_{0}_to_{1}", StartDate, EndDate)); }
Image may be NSFW.
Clik here to view.
Clik here to view.
