본문 바로가기
Computing

[개발].NET Core C# CSV/TEXT 파일 저장

by 플랜비맨매니져 2021. 8. 25.
var stream = new MemoryStream();
using (var sw = new StreamWriter(stream: stream, encoding: new UTF8Encoding(true)))
{
	StringBuilder sb = new StringBuilder();
    list.ForEach(x => {
    	List<string> line = new List<string>();
        line.Add(x.aa);
        line.Add(x.bb);
        line.Add(x.cc);
        line.Add(x.dd);
        
        sb.AppendLine(string.Join(",", line));
    });
    
    sw.WriteLine(sb.ToString());
    sw.Flush();
    var len = stream.Length;
    sw.Close();
    var toW = new byte[len];
    
    Array.Copy(stream.GetBuffer(), 0, toW, 0, len);
    return File(toW.ToArray(), "text/csv", "sample.csv");
}
728x90

댓글