Search code examples
c#excelcsvcsvhelper

Any way to change header row attributes using CsvHelper?


I'm using CsvHelper to write a program that outputs a bunch of data onto a CSV file. The header row is a row of about 35 columns. Here's some of the code here...

[CsvColumn(Name = "Lease Name", FieldIndex = 1)]
public string leaseName2 { get; set; }

[CsvColumn(Name = "Field Name", FieldIndex = 2)]
public string fieldName2 { get; set; }

[CsvColumn(Name = "Reservoir", FieldIndex = 3)]
public string reservoir2 { get; set; }

[CsvColumn(Name = "Operator", FieldIndex = 4)]
public string operator2 { get; set; }

[CsvColumn(Name = "County", FieldIndex = 5)]
public string county2 { get; set; }

[CsvColumn(Name = "State", FieldIndex = 6)]
public string state2 { get; set; }

[CsvColumn(Name = "Majo", FieldIndex = 7)]
public string majo2 { get; set; }

I was just wondering if I could, so to speak, change the columns width or change the background color of the cell or do anything to that matter. I've been searching the web for a while and have not come up with anything. Any help would be greatly appreciated.


Solution

  • Simple answer is, you can't do that with a CSV. That's not what it is or what it's meant for. It's just comma-delimited-values, that's it.

    If you want to control aesthetics, you'll need to choose a format that can support these options, such an actual excel (xls) format.

    This is why when you open a CSV in excel and try to save it with formatting, it warns you all formatting will be lost if you keep the .CSV format.