Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column headers in CSV using fileHelpers library?

Is there a built-in field attribute in the FileHelper library which will add a header row in the final generated CSV?

I have Googled and didn't find much info on it. Currently I have this:

DelimitedFileEngine _engine = new DelimitedFileEngine(T); _engine.WriteStream         (HttpContext.Current.Response.Output, dataSource, int.MaxValue); 

It works, but without a header.

I'm thinking of having an attribute like FieldTitleAttribute and using this as a column header.

So, my question is at which point do I check the attribute and insert header columns? Has anyone done something similar before?

I would like to get the headers inserted and use custom text different from the actual field name just by having an attribute on each member of the object:

[FieldTitleAttribute("Custom Title")] private string Name 

and maybe an option to tell the engine to insert the header when it's generated.

So when WriteStream or WriteString is called, the header row will be inserted with custom titles.

I have found a couple of Events for DelimitedFileEngine, but not what's the best way to detect if the current record is the first row and how to insert a row before this.

like image 887
Heinnge Avatar asked Oct 20 '10 07:10

Heinnge


People also ask

Can CSV files have headers?

A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. Initially, the CSV file is converted to a data frame and then a header is added to the data frame. The contents of the data frame are again stored back into the CSV file.


1 Answers

I know this is an old question, but here is an answer that works for v2.9.9

FileHelperEngine<Person> engine = new FileHelperEngine<Person>(); engine.HeaderText = engine.GetFileHeader(); 
like image 129
KiwiPiet Avatar answered Sep 21 '22 14:09

KiwiPiet