I'm using CSVHelper to write a List<> that I've obtained by using MySqlDataReader. My problem is with formatting my CSV file correctly, I've created a mapper class that should dictate where to place the data by column name:
public ReaderMap()
{
Map(m => m.accountpersonfirstname).Name("E-mail Address");
Map(m => m.accountpersonlastname).Name("First Name");
Map(m => m.emailaddress).Name("Last Name");
}
This comes directly from http://joshclose.github.io/CsvHelper/#mapping-name.
I'm going to assume that my issue is within how my streamwriter is writing to my existing csv as it seams to create a new CSV file each time (and I loose my names) however I've also tried using the mapping by index as well which should work on a new CSV file but it still puts the data right next to each other where I would like to see my columns a few columns apart so that the data is readable - I also need header names.
Here are my two classes - the mapper and the original data class.
class Reader
{
public string emailaddress { get; set; }
public string accountpersonfirstname { get; set; }
public string accountpersonlastname { get; set; }
}
class ReaderMap : CsvClassMap<Reader>
{
public ReaderMap()
{
Map(m => m.accountpersonfirstname).Name("E-mail Address");
Map(m => m.accountpersonlastname).Name("First Name");
Map(m => m.emailaddress).Name("Last Name");
}
}
I'm also going to paste part of my code because like I've mentioned above, perhaps I'm not using stream writer correctly (even though I think I am).
MySqlCommand cmd = new MySqlCommand(sqlCmd, cn);
cmd.CommandType = CommandType.Text;
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Reader dataExport = new Reader();
dataExport.accountpersonfirstname = rdr.GetString(0);
dataExport.accountpersonlastname = rdr.GetString(1);
dataExport.emailaddress = rdr.GetString(2);
dataList.Add(dataExport);
}
var textWriter = new StreamWriter(filePath);
var csv = new CsvWriter(textWriter);
csv.Configuration.RegisterClassMap<ReaderMap>();
foreach (var item in dataList)
{
csv.WriteRecord(item);
}
Maybe the issue is within my initiation of the streamwriter above or perhaps this all goes back to using the data reader - I'm not entirely sure. I hope someone can shed some light on this for me.
EDIT:
It seems using the following allowed it to append correctly. However my original issue still exists in the fact that when I move my header firstname and lastname on the csv file further over to column F and G it still places them at B and C as if it's completely ignoring my class and configuration that I have set.
var textWriter = new StreamWriter(filePath, true);