Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Objects to CSV

From Wiki

Jump to: navigation, search


Convert objects into a CSV string

Converts a IList collection into a CSV string. Converts object into a CSV string. Includes headers.

Uses reflection.

  1. /// <summary>
  2.         /// Return a CSV file in a string.
  3.         /// </summary>
  4.         /// <param name="collection">IList.</param>
  5.         /// <returns>string.</returns>
  6.         public static string CollectionToCsv(IList collection) {
  7.  
  8.             if (collection == null) throw new ArgumentNullException("collection", "Value can not be null or nothing!");
  9.  
  10.             StringBuilder sb = new StringBuilder();
  11.  
  12.             for (int index = 0; index < collection.Count; index++) {
  13.                 object item = collection[index];
  14.  
  15.                 if (index == 0) {
  16.                     sb.Append(ObjectToCsvHeader(item));
  17.                 }
  18.                 sb.Append(ObjectToCsvData(item));
  19.                 sb.Append(Environment.NewLine);
  20.             }
  21.  
  22.             return sb.ToString();
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Creates a comma delimeted string of all the objects property values names.
  27.         /// </summary>
  28.         /// <param name="obj">object.</param>
  29.         /// <returns>string.</returns>
  30.         public static string ObjectToCsvData(object obj) {
  31.             if (obj == null) {
  32.                 throw new ArgumentNullException("obj", "Value can not be null or Nothing!");
  33.             }
  34.  
  35.             StringBuilder sb = new StringBuilder();
  36.             Type t = obj.GetType();
  37.             PropertyInfo[] pi = t.GetProperties();
  38.  
  39.             for (int index = 0; index < pi.Length; index++) {
  40.                 sb.Append(pi[index].GetValue(obj, null));
  41.  
  42.                 if (index < pi.Length - 1) {
  43.                     sb.Append(",");
  44.                 }
  45.             }
  46.  
  47.             return sb.ToString();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Creates a comma delimeted string of all the objects property names.
  52.         /// </summary>
  53.         /// <param name="obj">object.</param>
  54.         /// <returns>string.</returns>
  55.         public static string ObjectToCsvHeader(object obj) {
  56.             if (obj == null) {
  57.                 throw new ArgumentNullException("obj", "Value can not be null or Nothing!");
  58.             }
  59.  
  60.             StringBuilder sb = new StringBuilder();
  61.             Type t = obj.GetType();
  62.             PropertyInfo[] pi = t.GetProperties();
  63.  
  64.             for (int index = 0; index < pi.Length; index++) {
  65.                 sb.Append(pi[index].Name);
  66.  
  67.                 if (index < pi.Length - 1) {
  68.                     sb.Append(",");
  69.                 }
  70.             }
  71.             sb.Append(Environment.NewLine);
  72.  
  73.             return sb.ToString();
  74.         }

325 Rating: 2.8/5 (16 votes cast)