If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Did you know.. How to create CSV or Text file in C#?

by Farooq Kaiser 23. February 2010 07:52

Here is our C# code to generate CSV file.

   1: StringBuilder sb = new StringBuilder();
   2: string strDelimiter = ",";
   3:  
   4: foreach (DataRow dr in table.Rows)
   5: {
   6:    string[] arr = Array.ConvertAll(dr.ItemArray, new Converter<object, string>(delegate(object obj)
   7:                  { return obj.ToString().Trim(); }));
   8:  
   9:     sb.AppendLine(string.Join(strDelimiter , arr));
  10: }
  11:  
  12: File.WriteAllText(outputFile, sb.ToString());

 

Please feel free to leave any comments. Thanks.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | C# | Did you know

Comments

Jobs Autos Real estate Videos Power by Google