Column headers in CSV using fileHelpers library?

Viewed 34091

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.

7 Answers

I don't know if you still need this, but here is the way FileHelper is working : To include headers of columns, you need to define a string with headers delimited the same way as your file. For example with '|' as delimiter :

 public const string HeaderLine = @"COLUMN1|COLUMN2|COLUMN3|...";

Then, when calling your engine :

DelimitedFileEngine _engine = new DelimitedFileEngine<T> { HeaderText = HeaderLine };

If you don't want to write the headers, just don't set the HeaderText attribute on the engine.

Just to include a more complete example, which would have saved me some time, for version 3.4.1 of the FileHelpers NuGet package....

Given

[DelimitedRecord(",")]
public class Person
{
   [FieldCaption("First")]
   public string FirstName { get; set; }

   [FieldCaption("Last")]
   public string LastName { get; set; }

   public int Age { get; set; }
}

and this code to create it

static void Main(string[] args)
{
    var people = new List<Person>();
    people.Add(new Person() { FirstName = "James", LastName = "Bond", Age = 38 });
    people.Add(new Person() { FirstName = "George", LastName = "Washington", Age = 43 });
    people.Add(new Person() { FirstName = "Robert", LastName = "Redford", Age = 28 });

    CreatePeopleFile(people);
}

private static void CreatePeopleFile(List<Person> people)
{
    var engine = new FileHelperEngine<Person>();

    using (var fs = File.Create(@"c:\temp\people.csv"))
    using (var sw = new StreamWriter(fs))
    {
        engine.HeaderText = engine.GetFileHeader();
        engine.WriteStream(sw, people);
        sw.Flush();
    }
}

You get this

First,Last,Age
James,Bond,38
George,Washington,43
Robert,Redford,28

You can simply use FileHelper's GetFileHeader function from base class

 var engine = new FileHelperEngine<ExportType>();
 engine.HeaderText = engine.GetFileHeader();                             
                            
 engine.WriteFile(exportFile, exportData);
Related