使用C#在CSV文件中写入

Was*_*RAR 3 .net c# csv string file

我正在寻找一种在CSV文件的不同单元格中编写字符串的方法.

我正在使用这个程序,

    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 ?
    }
Run Code Online (Sandbox Code Playgroud)

我需要的是在第一个单元格中写入"a",在第二个单元格中写入"b",c ..

Rei*_*ica 10

CSV是一种非常简单的文件格式,特别是如果您不需要任何单元格来包含嵌套逗号.CSV表示以逗号分隔的值,因此您只需要在每个单元格之间构造一个带逗号的字符串.这将有效,但它只适用于一行:

File.WriteAllText(filePath, String.Join(",", a, b, c, d));
Run Code Online (Sandbox Code Playgroud)