① 背景
需求:将测试项数据自动输出到Excel表格,用于统计分析。用CSV文件是因为操作简单方便,用 “,”表示分隔,用 “\n” 表示换行。
② 上代码
static void Main(string[] args)
{
string csvFileName = Directory.GetCurrentDirectory() + "\\" + "DATA.csv";
if (File.Exists(csvFileName))
{
File.Delete(csvFileName);
}
StreamWriter sw = new StreamWriter(csvFileName, true, Encoding.UTF8);
string head = "NO,A,B,C" + "," + "\n";
sw.Write(head);
string content1 = "1,a1,b1,c1" + "," + "\n";
sw.Write(content1);
string content2 = "2,a2,b2,c2" + "," + "\n";
sw.Write(content2);
sw.Flush();
sw.Close();
Console.ReadLine();
}