I'm using LinqToCSV to output a List to CSV. Snippet of sample code is as follows:
class Person
{
[CsvColumn(Name = "Name", FieldIndex = 1)]
public string UserName { get; set; }
[CsvColumn(Name = "Address 1", FieldIndex = 2)]
public string Address1 { get; set; }
[CsvColumn(Name = "Telephone", FieldIndex = 3)]
public string Telephone { get; set; }
}
private void OutputToCSV(string filenamePrefix, List<Person> people)
{
CsvFileDescription outputFileDescription = new CsvFileDescription
{
SeparatorChar = ','
FirstLineHasColumnNames = true,
FileCultureName = "en-GB"
};
CsvContext cc = new CsvContext();
cc.Write(
people,
@"C:\temp\people.csv",
outputFileDescription);
}
The issue I have is with the telephone number. If it is in the object as 0123456789012 then when I open the CSV in Excel it is seen as a number and the leading zero is removed. I'd like to pre format the column in the CSV as text.
According to Stop Excel from automatically converting certain text values to dates I can start the field with an equals and put the value in quotes, but is there an attribute I can set which will mean that LinqToCSV will do this for me? I don't really want to use LinqToCSV, then open the file and edit it to get it how I want.