1

So I just installed CsvHelper because that's the one I heard was the better one to use. I looked at the documentation and tried to figure out how to accompish what I wanted which is to create 1 column and fill it with values that are seperated with commas (,). And then a cell under that one that would have the corresponding value so like this

ID,Type,Name,Description,Image
11,Variation,MyCoolProduct,A super cool product, Image1 | Image2

I dont want different columns to the side I want to have 1 column with a string inside that is formated like that.

This is what I did, which didnt work because you cant even open the file because you get a SYLK format issue

var records = new List<Columns>
{
    new Columns {ID = 12, Type = "Variation", Description = "Simple product with different colors",
                 Images = "Image1 | Image 2", Price = 19.99d}
    };

using (StreamWriter sw = new StreamWriter("Testfile.csv"))
{
    var writer = new CsvWriter(sw);
    writer.WriteRecords(records);
}

UPDATE

I've mapped it like this now, how do i write it out to a textfile?

public sealed class MyClassMap : ClassMap<Columns>
    {
        public MyClassMap()
        {
            Map(m => m.ID);
        }
    }

1 Answer 1

1

You should generally create a CsvClassMap class and map your class to the CSV format you need.

It's really simple and has a nice fluent interface. Just do something like:

   public class ColMap : CsvClassMap<Column>
   {
       public ColMap()
       {
           Map(m=>m.ID).Name("ID").Index(0);
           .
           .
           .
       }
    }

Some of the other options after the .Index call will allow you to further configure the format of each of your columns.

Sign up to request clarification or add additional context in comments.

5 Comments

Is CsvClassMap something that comes with the library?
It is indeed. You also need to put the CSVWriter in a using, something I didn't notice before.
Ahh sorry, in the newer versions it's just called ClassMap, I forgot about that!
Updated my question with th classmap, how do i write it out to a csv file as I explained with like 1 column being 1 long string with the values seperated by a , ?
Sorry I don't understand. What you mean by 1 long column but also separated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.