写入csv文件中的下一列c#

mbu*_*ugr 0 c# csv excel

我有两个列表,每个列表都有不同的数据我想将列表数据添加到csv文件我的问题如何写在下一列.

这两个列表是dl,xl我想在列中写入xl而在另一列中写入dl

这是我的代码

using (StreamWriter sw = File.CreateText("list.csv"))
            {
                for (int i = 0; i < dl.Count; i++)
                {
                    // the two list have the same count 
                    sw.WriteLine(dl[i]);
                    sw.WriteLine(xl[i]); // i want to write this in another column 
                }
            }
Run Code Online (Sandbox Code Playgroud)

Joh*_* Wu 5

您没有编写Excel文件.您正在编写与Excel兼容的CSV文件.CSV代表"逗号分隔值",表示您应该使用逗号作为列分隔符.

所以...改变这个:

sw.WriteLine(dl[i]);
sw.WriteLine(xl[i]); // i want to write this in another column 
Run Code Online (Sandbox Code Playgroud)

对此:

var line = String.Format("{0},{1}", dl[i], xl[i]);
sw.WriteLine(line);
Run Code Online (Sandbox Code Playgroud)