3

I'm looking for a way to write strings in a different cell of a CSV file.

I'm using this program,

    private void button1_Click(object sender, EventArgs e)
    {
        string filePath = @"E:\test.csv";  

        string a = "a";
        string b = "c";
        string c = "d";
        string d = "d";

        File.WriteAllText(filePath, a);
        // how can add the other strings in the next cells ?
    }

What I need here is to write "a" in the first cell, "b" in the second cell, c ..

3 Answers 3

10

CSV is a pretty simple file format, especially if you don't need any cells to contain nested commas. CSV means comma-separated values, so you just need a way to construct a string with commas between each cell. This will work, although it is only for a single line:

File.WriteAllText(filePath, String.Join(",", a, b, c, d));
Sign up to request clarification or add additional context in comments.

5 Comments

-1 for String.Join, this would be completely ruined by any argument containing a line feed or reserved character.
@Chris - Yes, this does not take into account any of the details with quoting, commas, etc. But a simple solution might be what an absolute beginner could most easily use.
@Justin simple in this case however is wrong, especially in the case of a beginner. Using this as a solution once you encounter data with these characters you will start doing elaborate string manipulation to try to "fix" the problem which will be extremely brittle and probably repeated many times. The correct approach is to a use a library that respects all of this and is an exercise in configuration instead of algorithms.
+1 I dont agree with @Chris - use the simplest solution for the problem at hand, although the boundary conditions should be clear (and a reference to FileHelpers for more advanced scenarios certainly doesn't hurt)
@jeroenh I am absolute firm believer in KISS and YAGNI but there's a difference between simple and contrived, this sample is contrived not simple.
3

CSV is absolutely NOT a SIMPLE file format, it is a sufficiently elaborate format that is able to encompass almost any kind of data regardless of shape or size.

The CSV format is capable of dealing with optional parameters vs non optional, data with or without line breaks, and should be able to work with or without field escaping characters in any field without line breaks and is required to have field escaping characters in fields with line breaks.

You should not ever work with CSV files by hand, you should utilize FileHelpers to work with CSV files.

At this point I no longer use FileHelpers as my goto CSV parsing library I use CsvHelper from Josh Close.

5 Comments

The problem with CSV is that there isn't a fixed format (culture dependant) and some applications use their own implementation. Each implementation, however, is relatively simple. See en.wikipedia.org/wiki/Comma-separated_values for more information.
Is it possible using Excel file ?
@dotNET yes excel correctly supports all of those features when you open a CSV file, excel however is terrible at CREATING csv files. If I create a CSV file by hand I use open office calc (their excel), otherwise I generate them using FileHelpers and they can be consumed by excel without issue.
Do you have please a solution for this problem, because I treid csv, xlsx, xls but I'm getting the same result all the strings are added in the first cell !!
@dotNET i can absolutely tell that generating CSV files with FileHelpers open correctly in excel (i use filehelpers in production), if you edit your question and post the code you trying to interact with FileHelpers I'll be glad to point out anything I see wrong.
0

If there are only 4 values, and a single row? (Which I assume is not the case?)

string csv = string.Format("{0},{1},{2},{3}\n", a,b,c,d);
File.WriteAllText(filePath, csv);

If the data is based on some sort of collection, give some more info.

2 Comments

-1 this would be ruined by any argument that contains a line feed or reserved character.
Yes, this is not a general solution and definitely not intended to be the be-all solution for writing CSV files. In many cases however, constructing a CSV (or any delimited) file in this (or similar) manner is perfectly acceptable, as the the incoming data is in an expected or cleaned format. My solution (as well as Justin's) will work for the example in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.