Search code examples
c#csvhelper

Write data in specific position


I have the following code:


class Dto
{
    public string Data1 { get; set; }
    public string DataN { get; set; }
    public string DataN1 { get; set; }
}

class DtoMap : ClassMap<Dto>
{
    public DtoMap()
    {
        Map(x => x.Data1).Index(0);

        // Note that I need to skip N columns
        Map(x => x.DataN).Index(N);
        Map(x => x.DataN1).Index(N1);
    }
}

// create csv

using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var csvWriter = new CsvWriter(streamWriter, csvConfiguration))
{
    csvWriter.Configuration.RegisterClassMap<TMap>();
    csvWriter.WriteRecords(records);
    streamWriter.Flush();

    return memoryStream.ToArray();
}

Expected result is the following CSV table:

header 1 header ... header n header n + 1
Data 1 ... Data N Data N + 1
Data 2 ... Data N Data N + 1

Seems that indexes are ignored during writing csv file. Problem occurred in 15.0.6 and 26.0.0 versions. Any suggestions?


Solution

  • CsvHelper will output columns in the order specified by the indices, but it will not add empty columns for missing indices. If you need to do that, you can use Map().Index(i).Constant(""); to indicate a constant value should be used at a specific index i. Use that to fill in a constant empty value for the columns you want to leave empty:

    class DtoMap : ClassMap<Dto>
    {
        const int N = 12;
    
        public DtoMap() : this(true) { }
        
        public DtoMap(bool includeEmptyColumns)
        {
            Map(x => x.Data1).Index(0);
    
            if (includeEmptyColumns)
            {
                for (int i = 1; i < N; i++)
                {
                    Map().Index(i).Constant("");
                }
            }
    
            // Note that I need to skip N columns
            Map(x => x.DataN).Index(N);
            Map(x => x.DataN1).Index(N + 1);
        }
    }
    

    See: Constant Value.

    This leaves the column titles empty. If you need to provide some default column title you can specify one with .Name(string):

    Map().Index(i).Constant("").Name($"Column {i}");
    

    You will not be able to use this class map to read your CSV, however, because (as of CsvHelper 26.1.0 at least) the reader will throw an error when a column is not mapped to a class member even if it is constant. To work around the error, use new DtoMap(false) when reading, i.e.:

    var newRecords = ReadCSV(csv, new DtoMap(false), config);
    
    static List<TRecords> ReadCSV<TRecords>(byte [] data, ClassMap<TRecords> map, CsvConfiguration csvConfiguration)
    {
        using (var memoryStream = new MemoryStream(data))
        using (var streamWriter = new StreamReader(memoryStream))
        using (var csvReader = new CsvReader(streamWriter, csvConfiguration))
        {
            csvReader.Context.RegisterClassMap(map);
            return csvReader.GetRecords<TRecords>().ToList();
        }
    }
    

    Demo fiddles here and here.