简体   繁体   English

如何将值添加到现有的.csv文件

[英]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. 我在.csv文件中有一个信息,只需单击一下按钮,我便可以在该数字上加1。

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 因此,以Laymans的术语讲,当我按下按钮时,如何将1加到选定的列中

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 老实说,我没有尝试过,因为我不知道如何在Google上搜索它,如果有任何问题请询问,我必须在3个小时内提出来

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? 您如何呈现.csv文件的当前值? How you are selecting the column and row you want to edit. 您如何选择要编辑的列和行。

Regards. 问候。

Well you can try using some libraries for parsing CSV files. 好吧,您可以尝试使用某些库来解析CSV文件。 The one I am using is: THIS ONE . 我正在使用的是: THIS ONE It has CSV writer and reader, easy to use and for my purposes its perfect. 它具有CSV编写器和读取器,易于使用,并且对我而言非常完美。 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 :) 给我发送电子邮件,我将尽力帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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