■ CsvWriter 클래스의 WriteRecords 메소드를 사용해 CSV 파일을 생성하는 방법을 보여준다.
▶ Student.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using CsvHelper.Configuration.Attributes; namespace TestProject; /// <summary> /// 학생 /// </summary> public class Student { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> [Name("ID")] public string ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> [Name("성명")] public string Name { get; set; } #endregion #region 점수 - Score /// <summary> /// 점수 /// </summary> [Name("점수")] public int Score { get; set; } #endregion } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using System.Globalization; using CsvHelper; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<Student> sourceList = new List<Student> { new Student { ID = "ID001", Name = "홍길동", Score = 90 }, new Student { ID = "ID002", Name = "김철수", Score = 100 }, new Student { ID = "ID003", Name = "이영희", Score = 80 } }; using(StreamWriter streamWriter = new StreamWriter("d:\\result.csv")) { using(CsvWriter csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture)) { csvWriter.WriteRecords(sourceList); } } } #endregion } |