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());
}
......
3
  • Move File.AppendAllText() outside the loop Commented Mar 1, 2018 at 16:06
  • @MathiasR.Jessen, i tried that. The file does not get written. Commented Mar 1, 2018 at 16:08
  • Based on title this is duplicate of stackoverflow.com/questions/2537823/…, but really is just standard "how to write CSV to file in C#" like stackoverflow.com/questions/18757097/… 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) Commented Mar 1, 2018 at 16:42

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the help. I will read up on streamwriter and stringbuilder.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.