Column headers in CSV using filesHelpers?

Is there a built-in field attribute in the FileHelper library that will add a title bar to the final generated CSV?

I have Googled and have not found much information about this. I currently have this:

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

It works, but without a title.

I am thinking of having an attribute of type FieldTitleAttribute and using it as a column heading.

So my question is at what point do I check the attribute and insert the header columns? Has anyone done something like this before?

I would like the headers to insert and use custom text different from the actual field name, just using the attribute for each member of the object:

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

and, possibly, the ability to tell the engine to insert a title when creating it.

Therefore, when WriteStream or WriteString , a header row will be inserted using custom headers.

I found a couple of events for DelimitedFileEngine, but not the best way to determine if the current record is the first row and how to insert the row before that.

+45
c # csv filehelpers
Oct 20 2018-10-10T00:
source share
5 answers

Here is the code that will do this: https://gist.github.com/1391429

To use it, you must decorate your [FieldOrder] fields (in any case, good practice FileHelpers). Using:

 [DelimitedRecord(","), IgnoreFirst(1)] public class Person { // Must specify FieldOrder too [FieldOrder(1), FieldTitle("Name")] string name; [FieldOrder(2), FieldTitle("Age")] int age; } ... var engine = new FileHelperEngine<Person> { HeaderText = typeof(Person).GetCsvHeader() }; ... engine.WriteFile(@"C:\people.csv", people); 

But support for this really needs to be added in the FileHelpers themselves. I can come up with a few design questions from the top of my head that would need to be answered before they can be implemented:

  • What happens when reading a file? Afaik FileHelpers is currently all based on the ordinal position of the column and ignores the column names ... but if we now have the [FieldHeader] attributes, should we also try to match the properties with the column names in the file? Should an exception be thrown if they do not match? What happens if the ordinal position is not consistent with the column name?
  • When reading as a data table, you should use A) the field name (current project) or B) the column name of the source file or C) the FieldTitle attribute?
+29
Nov 24 2018-11-11T00:
source share

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(); 
+31
Mar 18 '14 at 19:27
source share

I don’t know if you need this problem, but here is how FileHelper works: To include column headers, you need to define a row with headers that are separated just like your file. For example, with '|' as a separator:

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

Then when calling your engine:

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

If you do not want to write headings, just do not set the HeaderText attribute in the engine.

+23
Jan 28 '11 at 11:19
source share
 List<MyClass> myList = new List<MyClass>(); FileHelperEngine engine = new FileHelperEngine(typeof(MyClass)); String[] fieldNames = Array.ConvertAll<FieldInfo, String>(typeof(MyClass).GetFields(), delegate(FieldInfo fo) { return fo.Name; }); engine.HeaderText = String.Join(";", fieldNames); engine.WriteFile(MapPath("MyClass.csv"), myList); 
+4
Dec 09 '13 at 10:11
source share

I found that you can use FileHelperAsyncEngine to accomplish this. Assuming your data is an "output" list of type "outputData", you can write code that looks like this:

  FileHelperAsyncEngine outEngine = new FileHelperAsyncEngine(typeof(outputData)); outEngine.HeaderText = "Header1, Header2, Header3"; outEngine.BeginWriteFile(outputfile); foreach (outputData line in output){ outEngine.WriteNext(line); } outEngine.Close(); 
+1
Jun 29 '11 at 23:52
source share



All Articles