Reputation: 1067
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);
}
}
Upvotes: 1
Views: 305
Reputation: 993
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.
Upvotes: 1