编辑/保存CSV文件中的行

时间:2013-01-17 01:20:29

标签: c# asp.net csv

关注此topic之后我能够创建 新的行,但我的问题是如何做 我将新行保存或写入文件?

我试过'StreamWriter',但它只写了新的 创建了一行。

有什么建议吗?

到目前为止,这是我的代码:

string path = @"C:/CSV.txt";

string[] lines = File.ReadAllLines(path);

var splitlines = lines.Select(l => l.Split(','));

foreach (var line in splitlines)
{
   if(line[1].Contains("34"))
   {                                        
     line[1] = "100";
     var newline = string.Join(",", line);
     StreamWriter sr = new StreamWriter(path);    
     sr.WriteLine(newline);
     sr.Close();                                      
   }
 }

3 个答案:

答案 0 :(得分:9)

以下是使用StreamReader类:

的解决方案
String path = @"C:\CSV.txt";
List<String> lines = new List<String>();

if (File.Exists(path));
{
    using (StreamReader reader = new StreamReader(path))
    {
        String line;

        while ((line = reader.ReadLine()) != null)
        {
            if (line.Contains(","))
            {
                String[] split = line.Split(',');

                if (split[1].Contains("34"))
                {
                    split[1] = "100";
                    line = String.Join(",", split);
                }
            }

            lines.Add(line);
        }
    }

    using (StreamWriter writer = new StreamWriter(path, false))
    {
        foreach (String line in lines)
            writer.WriteLine(line);
    }
}

如果要覆盖该文件,请将this StreamWriter构造函数与append = false一起使用。

答案 1 :(得分:1)

也许是这样的,你可能需要清理它并添加一些错误处理,但是它可以完成这项工作

    string path = @"C:\\CSV.txt";

    string[] lines = File.ReadAllLines(path);
    for (int i = 0; i < lines.Length; i++)
    {
        string line = lines[i];
        if (line.Contains(","))
        {
            var split = line.Split(',');
            if (split[1].Contains("34"))
            {
                split[1] = "100";
                line = string.Join(",", split);
            }
        }
    }
    File.WriteAllLines(@"C:\\CSV.txt", lines);

答案 2 :(得分:0)

        string str;
        string PID;
        string PN;
        string UP;
        string DIS;
        string STK;
        string CSVFilePathName = @"C:\admin.csv";
        string[] Lines = File.ReadAllLines(CSVFilePathName);
        File.Delete(CSVFilePathName);
        StreamWriter sw = new StreamWriter(CSVFilePathName", true);

            PID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text;
            PN = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
            UP = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
            DIS = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
            STK = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text;
        foreach (string li in Lines)
        {
            string[] Fields = li.Split(',');
            if(PID==Fields[0])
            {                                                                     
                str = PID + "," + PN + "," + UP + "," + DIS + "," + STK;                                 
            }
            else
            {
                str = Fields[0] + "," + Fields[1] + "," + Fields[2] + "," + Fields[3] + "," + Fields[4];                 
            }
            sw.WriteLine(str);
        }
        sw.Flush();
        sw.Close();
相关问题