■ 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 43 44 45 46 47 48 |
using System.Globalization; using CsvHelper; using CsvHelper.Configuration; 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 = "ID004", Name = "김동수", Score = 80 } }; CsvConfiguration csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture); csvConfiguration.HasHeaderRecord = false; using(FileStream fileStream = File.Open("source.csv", FileMode.Append)) { using(StreamWriter streamWriter = new StreamWriter(fileStream)) { using(CsvWriter csvWriter = new CsvWriter(streamWriter, csvConfiguration)) { csvWriter.WriteRecords(sourceList); } } } } #endregion } |