0

I am trying to find a better way to write data to csv file. Right now every loop iteration appends the headers and data to file like below

enter image description here

I want to modify my logic so that I get only the data like.

enter image description here

Any tips on what I can change are highly appreciated. Thanks

......
var csvContent = new StringBuilder();
csvContent.AppendLine("ONE, TWO, THREE);
var csvPath = defaultFilePath + "file7.csv";
foreach (var i in values)
{
    fileData.DataOne = i["DataPointOne"].ToString();
    fileData.DataTwo = i["DataPointTwo"].ToString();
    fileData.DataThree = DateTime.Parse(i["DataPointThree"]);
    csvContent.AppendLine(fileData.DataOne + "," + fileData.DataTwo + "," + fileData.DataThree);
    File.AppendAllText(csvPath, csvContent.ToString());
}
......
Maddy
  • 2,025
  • 5
  • 26
  • 59
  • Move `File.AppendAllText()` outside the loop – Mathias R. Jessen Mar 01 '18 at 16:06
  • @MathiasR.Jessen, i tried that. The file does not get written. – Maddy Mar 01 '18 at 16:08
  • Based on title this is duplicate of https://stackoverflow.com/questions/2537823/distinct-by-property-of-class-with-linq, but really is just standard "how to write CSV to file in C#" like https://stackoverflow.com/questions/18757097/writing-data-into-csv-file-in-c-sharp which shows exactly the same code you trying to do, just with proper placement of writing result of building string to file. (Likely you are not doing it anyway for actual code and using a library to do read/write CSV, but it is ok to ask questions for entertainment/education purposes) – Alexei Levenkov Mar 01 '18 at 16:42

2 Answers2

4

You constantly append lines to csvContent and then write it all to the file with every iteration. Get rid of the StringBuilder (it isn't really needed here) and use a StreamWriter like this:

using(var streamWriter = new StreamWriter("path\\toFile.csv")) 
{
    streamWriter.WriteLine("ONE, TWO, THREE");
    foreach (var i in values)
    {
        fileData.DataOne = i["DataPointOne"].ToString();
        fileData.DataTwo = i["DataPointTwo"].ToString();
        fileData.DataThree = DateTime.Parse(i["DataPointThree"]);
        streamWriter.WriteLine(fileData.DataOne + "," + fileData.DataTwo + "," + fileData.DataThree);
    }
}
waka
  • 3,362
  • 9
  • 35
  • 54
1

Problem is that there is same StringBuilder content is stored into file multiple times.

Iteration 1 csvContent contains data for line 1 --> line 1 is stored to file. File contains line 1

Iteration 2 csvContent is appended with line 2 --> line 1, 2 is appended stored into file. File contains line 1, 1, 2

Iteration 3 csvContent is appended with line 3 --> line 1, 2, 3 is again appended to file. File contains line 1, 1, 2, 1, 2, 3

Simple fix should be to store the file outside the loop or use e.g. StreamWriter suggested in the other answer.

mybrave
  • 1,662
  • 3
  • 20
  • 37