CSVHelper - how to write Multiple Header Rows

Viewed 11

I am working with Amazon Uploader Template Files that have three header rows, where the second and third are column specific and the first row is also lined up to some degree.

I need to be able to write these three headers out properly when I write the data to a CSV file and I cannot find any means by which to control this.

I have an Object and Map configured for the third row, but I have no idea how to create the first two rows. The second and third row columns must match, the first row must always be as the original was presented.

In other words, as long as the column headers from the second and third row are together the column order of these two rows can change, but the first row must always be the same.

I cannot find anything in CSVHelper that will manage this the way I need to. Is there a means by which I can manage these two header rows via CSVHelper?

1 Answers

You can write a row, including a header row, any way you like using the WriteField method.

csv.WriteField("First");
csv.WriteField("Row");
csv.WriteField("Headers");
csv.NextRecord();

If you have a class that you want the headers written for, you can use the WriteHeader method.

csv.WriteHeader<RowClass>();
csv.NextRecord();

You will need to specify the order of the fields written by either putting an [Index(int index)] attribute on the property or by creating a map and mapping the property to an index.

Write your 3 headers using one of the above methods, and then you can write the rows as you normally would.

csv.WriteRecords(records);
Related