CSVHelper not formatting CSV

2.2k Views Asked by At

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);
1

There are 1 best solutions below

0
On BEST ANSWER

When using the a custom CsvClassMap, the output is written in a Property per Column fashion without gaps unless a Property is ignored, in which case, the property is not output, but a gap is not created where it is ignored.

In order to create the gaps that you desire, you would need to create a custom method that writes out the file in the format you would desire.

//Write out the header
var csv = new CsvWriter(textWriter);
csv.WriteField("A Column Data");
csv.WriteField("B (Empty)");
csv.WriteField("C (Empty)");
csv.WriteField("D (Empty)");
csv.WriteField("E (Empty)");
csv.WriteField("F Column Data");
csv.WriteField("G Column Data");
csv.NextRecord(); //Newline

foreach (var item in dataList)
{
    csv.WriteRecord(item.PropertyA);
    csv.WriteRecord(string.Empty);
    csv.WriteRecord(string.Empty);
    csv.WriteRecord(string.Empty);
    csv.WriteRecord(string.Empty);
    csv.WriteRecord(item.PropertyB);
    csv.WriteRecord(item.PropertyC);
    csv.NextRecord();
}

Having said all that, why are you trying to insert gaps? Inserting gaps doesn't make a csv more readable, and if you are opening it in excel or some other spreadsheet app, double click on the right header border and it will auto expand the column width.