Writing to a CSV file using C #

I am looking for a way to write lines in another cell of a CSV file.

I use 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 ?
    }

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

+3
source share
3 answers

CSV is absolutely NOT a simple file format, it is a well-thought-out format that can cover almost any data, regardless of shape and size.

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

CSV , FileHelpers CSV.

FileHelpers CSA, CsvHelper Josh Close.

+3

CSV - , , . CSV , , . , :

File.WriteAllText(filePath, String.Join(",", a, b, c, d));
+10

If there are only 4 values ​​and one row? (What I guess is wrong?)

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

If the data is based on a collection, provide additional information.

0
source

All Articles