Search code examples
c#csvhelper

How to efficiently write a csv file with thousands of columns using csvhelper?


I am working on measurement software from which results are written to a huge list of lists which I am trying to put into .csv file using csvhelper. Problem is that to keep things fairly readable and I need to create thousands of columns and I don't see any simple and effective way to do it.

How can I create a .csv file which consist, let's say, 2000 columns?

EDIT 1. Sorry for my inaccurate post (it was my first ever made on StackOverflow).

Problem I am trying to solve is to create a .csv file with thousands of columns while using only a couple of lines of code so that a program will be as simple as possible. The main reason why I want to do it that way is to keep file readable by different software for data analysis.


Solution

  • What about adding properties to an ExpandoObject() (perhaps programatically in a loop) and then writing it out like so:

    void Main()
    {
        var records = new List<dynamic>();
    
        dynamic record = new ExpandoObject();
        record.Id = 1;
        record.Name = "one";
        records.Add(record);
    
        using (var writer = new StringWriter())
        using (var csv = new CsvWriter(writer))
        {
            csv.WriteRecords(records);
    
            writer.ToString().Dump();
        }
    }
    

    https://joshclose.github.io/CsvHelper/examples/writing/write-dynamic-objects