-1

So in my program the user can choose a file with an OpenFileDialog and if he wants to save the file with a SaveFileDialog, the columns and rows of the csv file should change. For this I have already tried this SaveFileDialog:

 List<String> liste = new List<string>();
        
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Filter = "CVS (*.cvs)|*.csv|All files (*.*)|*.*";

        if (dialog.ShowDialog() == true)
        {
            int counter = 0;
            string line;

            // Read the file and display it line by line.  
            try
            {
                System.IO.StreamReader file = new System.IO.StreamReader(path);
                while ((line = file.ReadLine()) != null)
                {
                    if (counter == 0)
                    {
                        line += ",column1,column2";
                        liste.Add(line + "\n");
                    }
                    else
                    {
                        line += $",{"fg"},{"gg"}";
                        liste.Add(line + "\n");                      
                    }

                    counter++;
                }

                File.WriteAllLines(dialog.FileName, liste);
                file.Close();
            }
            catch
            {
                MessageBox.Show("Der Gewählte Prozess wird bereits von einem anderen verwendet,\n " +
                    " bitte versuchen sie es erneut");
            }

However, I cannot change any columns or cells in this way, because it always appends the new lines to the end of the lines. So how do I manage that in the code behind I have the Csv file like this:

Picture1

after the Change to this:

Picture2

WPFHelp
  • 15
  • 5

1 Answers1

0

If I understand your question correctly. You need to parse csv file line by line to access each cell, then you can modify each cell and create another csv file.

For example this code change second column if exist.

        while ((line = file.ReadLine()) != null)
        {
            var cellArray = Regex.Split(line, "[\t,](?=(?:[^\"]|\"[^\"]*\")*$)")
            .Select(s => Regex.Replace(s.Replace("\"\"", "\""), "^\"|\"$", "")).ToArray();

            if (counter == 0)
            {
                if (cellArray.Length > 1)
                    cellArray[1] = "Changed Header";
            }
            else
            {
                if (cellArray.Length > 1)
                    cellArray[1] = "Changed Value";
            }

            liste.Add(string.Join(",", cellArray) + "\n");

            counter++;
        }

Regex credit goes to @aquinas from his answer at https://stackoverflow.com/a/11365961/5964792

MrMoeinM
  • 1,728
  • 1
  • 9
  • 13
  • It doesn't really help me because if you look at the pictures of my question you can see that the text from the heading was changed after the "conversion". How do I need to change your Code to change the header like Picture 2? And how do I fill a specific column? So in the end it looks like Picture 2. – WPFHelp Jun 17 '21 at 10:52