How to create a .CSV file in C# ?
-
Wednesday, July 09, 2008 3:40 AM
Hi
I have a 2 dimensional array as my final output from the application (designed in c#) now I need to create .CSV file to store this value, Could anyone tell me how do I do this in C# ?
Reply awaited.
Thanks.
SID
All Replies
-
Wednesday, July 09, 2008 4:46 AM
I hope this can get you started...
1 string filePath = @"C:\test.csv"; 2 string delimiter = ","; 3 4 string[][] output = new string[][]{ 5 new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"}, 6 new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"} 7 }; 8 int length = output.GetLength(0); 9 StringBuilder sb = new StringBuilder(); 10 for (int index = 0; index < length; index++) 11 sb.AppendLine(string.Join(delimiter, output[index])); 12 13 File.WriteAllText(filePath, sb.ToString()); -
Wednesday, July 09, 2008 6:39 AM
Thanks, a lot. Just got the output as .csv
SID -
Sunday, June 28, 2009 10:52 PMDennis,
While your code is very helpful, do you know of of any samples available for reading and writing(especially) to CSV files?
thank you,
Neil
neil -
Tuesday, December 28, 2010 9:26 PMThere is some free code that does this at http://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c
Jonathan Wood • Black Belt Coder

