简体   繁体   中英

How to add a value to a existing .csv file

I have information in a .csv file that I at a click of a button, I could just add 1 to that number.

So say this is my data table:

Years last paid:
2012
2012
2012
2012

and at a click of a button, on the selected columns

Years last paid: 
2013
2013
2012
2013 

So in Laymans terms, how do I just add 1 to a selected column when I press a button

I honestly dont have an attempt because I dont know how to google search it, if there are any questions please ask, I have to be up in 3 hours to present this

var lines=File.ReadAllLines(path)
              .Select(x=>Regex.IsMatch(x,@"\d+")?(int.Parse(x)+1).ToString():x);
File.WriteAllLines(path,lines);

try this

List<string> str = File.ReadAllText(@"completefilepath").Split('\n').ToList();
List<string> updatedValues =   new List<string>();
foreach (var value in str)
{
      int val = 0;
      if (int.TryParse(value, out val))
      {
         updatedValues.Add((val + 1).ToString());        
      }
}

File.WriteAllLines(@"completefilepath", updatedValues);
private static void UpdateFile(int selected)
    {
        var lines = File.ReadAllLines("test.csv");
        lines[selected] = (Convert.ToInt32(lines[selected]) + 1).ToString();
        File.WriteAllLines("test.csv", lines);
    }

How are you presenting the current values of the .csv file? How you are selecting the column and row you want to edit.

Regards.

Well you can try using some libraries for parsing CSV files. The one I am using is: THIS ONE . It has CSV writer and reader, easy to use and for my purposes its perfect. This just a suggestion I am not sure if it is going to work for your case but you can see the example in the website.I hope I helped :) Good luck with the project. I you decide to use this library and you have some difficulties. Send me an email and I will try to help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM