Search code examples
c#winformsexport-to-csvmultiline

Exporting mutiline data into same cell in csv


I understand that this question have been asked many times (1, 2 & 3) but I just don't understand how to apply it in my case. I have tried playing around for hours but I cannot get it right.

I have variables in the form of List<string>where each list contain datas that have line breaks between them/multiline data. Then I called an event that would export these datas in a CSV file. Below is my code.

savestate.cs - class where I initialized the variables

public partial class Savestate
 { 
    public static List<string> rtb1_list = new List<string>();
    public static List<string> rtb2_list = new List<string>();
    public static List<string> rtb3_list = new List<string>();
    public static List<string> rtb4_list = new List<string>();
 }

Form1.cs - The event

public void Savetocsv()
    {
      Type s = typeof(Savestate);
      FieldInfo[] fields = s.GetFields(BindingFlags.Public | BindingFlags.Static);

      StringBuilder csvdata = new StringBuilder();
      string header = String.Join(",", fields.Select(f => f.Name).ToArray());
      csvdata.AppendLine(header);

      string rtb1 = String.Join(",", Savestate.rtb1_list.ToArray());
      string rtb2 = String.Join(",", Savestate.rtb2_list.ToArray());
      string rtb3 = String.Join(",", Savestate.rtb3_list.ToArray());
      string rtb4 = String.Join(",", Savestate.rtb4_list.ToArray());

      string newlinestring = string.Format("{0}; {1}; {2}; {3}", rtb1, rtb2, rtb3, rtb4);
      csvdata.AppendLine(newlinestring);

      string filepath = @"C:\new.csv";
      File.WriteAllText(filepath, csvdata.ToString());
    }

However when I opened the CSV file, the words are all over the place. For example I wrote hi then a new line then I wrote bye. This is the actual output and this is my intended output.Hope that I can get help.


Solution

  • To insert line breaks in csv file you need to surround string with double quotes, so desired output is generated by following code :

    public partial class Savestate
    {
        public static List<string> rtb1_list = new List<string>() { "hi1", "bye1" };
        public static List<string> rtb2_list = new List<string>() { "hi2", "bye2" };
        public static List<string> rtb3_list = new List<string>() { "hi3", "bye3" };
        public static List<string> rtb4_list = new List<string>() { "hi4", "bye4" };
    }
    public static void Savetocsv()
    {
        Type s = typeof(Savestate);
        FieldInfo[] fields = s.GetFields(BindingFlags.Public | BindingFlags.Static);
    
        StringBuilder csvdata = new StringBuilder();
        string header = String.Join(",", fields.Select(f => f.Name).ToArray());
        csvdata.AppendLine(header);
    
        string rtb1 =  String.Join("\n", Savestate.rtb1_list.ToArray());
        string rtb2 =  String.Join("\n", Savestate.rtb2_list.ToArray());
        string rtb3 =  String.Join("\n", Savestate.rtb3_list.ToArray());
        string rtb4 =  String.Join("\n", Savestate.rtb4_list.ToArray());
    
        string newlinestring = string.Format("\"{0}\",\" {1}\",\" {2}\",\" {3}\"", @rtb1, @rtb2, @rtb3, @rtb4);
        csvdata.AppendLine(newlinestring);
    
        string filepath = @"new.csv";
        File.WriteAllText(filepath, csvdata.ToString());
    }
    

    Output file:

    enter image description here