Manipulate existing CSV file, while keeping columns order. (CsvReader/CsvWriter)

前端 未结 1 1654
闹比i
闹比i 2021-01-22 04:39

I need to manipulate an existing CSV file via following actions:

  1. Read from an existing CSV file -> then Append new row to it. I have following code which is chokin
相关标签:
1条回答
  • 2021-01-22 05:07

    If you want to append a record to a file, the best way to do it is read the items, add the new one to the collection, and write everything back.

    public static void Append(Customer customer, string file)
    {
        List<Customer> records = null;
        using (var reader = new StreamReader(file))
        {
            using (var csv = new CsvReader(reader))
            {
                records = csv.GetRecords<Customer>().ToList();
            }
        }
        records.Add(customer);
    
        using (var writer = new StreamWriter(file))
        {
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteRecords(records);
            }
        }
    }
    

    As @Dour High Arch mentioned, to be perfectly safe though you might want to take the extra step of using a temp file in case something goes wrong.

    If you want to update instead of append, you'd have to look up the specified record, and update it if it exists.

    public static void Update(Customer customer, string file)
    {
        List<Customer> records = null;
        using (var reader = new StreamReader(file))
        {
            using (var csv = new CsvReader(reader))
            {
                records = csv.GetRecords<Customer>().ToList();
            }
        }
        var index = records.FindIndex(x => x.ID == customer.ID);
        if (index >= 0)
        {
            records[index] = customer;
            using (var writer = new StreamWriter(file))
            {
                using (var csv = new CsvWriter(writer))
                {
                    csv.WriteRecords(records);
                }
            }
        }
    }
    

    Again, writing to a temp file is advisable.


    UPDATE

    Actually there's a slightly better way to append if you don't want to replace the file. When instantiating a StreamWriter you can do so with append=true. In which case, it will append to the end of the file. The small caveat is that in case the EOF marker is not at a new line but at the last field of the last record, this will append record to the end of the last field messing up your columns. As a workaround I've added a writer.WriteLine(); before using the CSVHelper class' writer.

    public static void Append2(Customer customer, string file)
    {
        using (var writer = new StreamWriter(file, true))
        {
            writer.WriteLine();
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteRecord(customer);
            }
        }
    }
    

    In case the file is in a new line, then this will add an empty line though. That can be countered by ignoring empty lines when you read a file.

    0 讨论(0)
提交回复
热议问题