none
How to generate new column in .csv file for show result True and False RRS feed

  • Question

  • hi to all,
    I want to how to show True or False Boolean value in .csv[ Excel ] file . Generate a new column for Show boolean value True/False. in C# .
     
    Thanks.

    A.Acharya Feedback to us Develop and promote your apps in Windows Store Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi ,
    I am using one text file in text file using 3 file path
    Like i have file.txt this file contain 3 file path 
     
    C :\user\dextop\abc.xml
    C:\user\dextop\xyz.xml   [both file is xml data contain]
     
    D:\list.csv     [  this file is show mismatch data of both xml file ]
    This is dynamically generated file
     
    I want to create a new column "Result"  in .csv file  for show result true or false
     
     btnCompaire_Click this function i am using  this function .
     
    How to create new column in .csv file using and modified this code ?
     
    private void btnCompaire_Click(object sender, RoutedEventArgs e)
    {
    List<string> fileslist = new List<string>();
    var list = new List<KeyValuePair<string, string>>();

    var secondlist = new List<KeyValuePair<string, string>>();

    if (File.Exists(txtpath))
    {
    string[] LinesinText = File.ReadAllLines(txtpath);
    fileslist.Add(LinesinText[0]);
    fileslist.Add(LinesinText[1]);
    fileslist.Add(LinesinText[2]);
    }
    string[] Linesin1 = File.ReadAllLines(fileslist[0]);
    string[] Linesin2 = File.ReadAllLines(fileslist[1]);
    bool areEqual = Linesin1.SequenceEqual(Linesin2);
    if (!areEqual)
    {
    int firstcountdata = Linesin1.Count();
    int secondcountdata = Linesin2.Count();
    int finalcount;

    if (firstcountdata > secondcountdata)
    {
    finalcount = firstcountdata;
    }
    else
    {
    finalcount = secondcountdata;
    }

    if (!areEqual)
    {
    for (int i = 0; i < finalcount; i++)
    {
    try
    {
    if (Linesin1[i] != null && Linesin2[i] != null)
    {
    if (Linesin1[i].Equals(Linesin2[i]))
    {

    }
    else
    {
    list.Add(new KeyValuePair<string, string>(Linesin1[i], Linesin2[i]));
    }
    }
    }
    catch (System.IndexOutOfRangeException)
    {
    if (firstcountdata > secondcountdata)
    {
    list.Add(new KeyValuePair<string, string>(Linesin1[i], "Not Exists"));
    }
    else
    {
    list.Add(new KeyValuePair<string, string>("Not Exists", Linesin2[i]));
    }
    continue;
    }
    catch (Exception msg)
    {
    System.Windows.MessageBox.Show(msg.Message);
    }
    }
    }

    var a = list[0];
    var aa = a.Key;
    var f = aa.Split(' ');
    var name = f[5];
    var value = f[6];

    var NameList = new List<string>();
    var ValueList = new List<string>();
    var firstlistdata = new List<string>();
    var secondlistdata = new List<string>();
    var thirdlistdata = new List<string>();

    List<KeyValuePair<string, string>> FirstListdata = new List<KeyValuePair<string, string>>();
    List<KeyValuePair<string, string>> SecondListdata = new List<KeyValuePair<string, string>>();

    var finaldata = new List<List<KeyValuePair<string, string>>>();

    using (StreamWriter sw = File.CreateText(fileslist[2]))
    {
    for (int i = 0; i < list.Count; i++)
    {
    var a1 = list[i].Key;
    var a2 = list[i].Value;

    string[] FirstList = a1.Split(' ');
    string[] SecondList = a2.Split(' ');


    foreach (var item in FirstList)
    {
    if (item.Contains("name"))
    {
    NameList.Add(((item.Split('='))[1]));
    }

    //if (item.Contains("value"))
    //{
    // ValueList.Add(item);
    //}
    }
    firstlistdata.Add(NameList[i]);

    foreach (var item in SecondList)
    {
    if (item.Contains("name"))
    {
    NameList.Add((item.Split('='))[1]);
    }
    //if (item.Contains("value"))
    //{
    // ValueList.Add(item);
    //}
    }
    secondlistdata.Add(NameList[i]);

    thirdlistdata = firstlistdata.Concat(secondlistdata).ToList();
    sw.WriteLine(thirdlistdata[i]);
    }
    }
    System.Windows.MessageBox.Show("Generated sucessfuly");
    }
    else
    {
    System.Windows.MessageBox.Show("Files are Equal");
    }
    }

    Monday, November 20, 2017 5:17 AM

All replies

  • Hi Anirddhan,

    Thank you for posting here.

    For your question, the code you provided has many compile errors. According to your description, I make a simple example for reference.

      string filePath = "test.csv";
                string delimiter = ",";
    
                string[][] output = new string[][]{
                      new string[]{"A",  "True"},
                       new string[]{"B",  "False"}
                   };
                int length = output.GetLength(0);
                StringBuilder sb = new StringBuilder();
                for (int index = 0; index < length; index++)
                    sb.AppendLine(string.Join(delimiter, output[index]));
    
                File.WriteAllText(filePath, sb.ToString());

    Where you store the data with bool column? If you have more questions about this, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 22, 2017 5:47 AM
    Moderator