I am creating a CSV file using C# this contains 200 headers. I'm getting the data from another CSV file which contains 150 headers. My problem is how I'm going to place the data according to its header. For instance I'm giving and example below.
The CSV file which will be created with C#:
Name, Surname, Locality, DateOfbirth, Age
Joe, Smith, 60
Sam, Brown, 20
The CSV getting the data from
Name, Surname, Age
Joe, Smith, 60
Sam, Brown, 20
This is a sample code (the actual files contains 150 header, and the new CSV file contains 200 headers)
string[] lines = System.IO.File.ReadAllLines(fileUrl);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileUrl))
{
foreach (string line in lines)
{
if (line == lines[0])
{
//Changing the header of the first file
file.WriteLine("Name, Surname, Locality, DateOfBirth, Age");
}
else
{
string[] values = line.Split(',');
file.WriteLine(string.Format("{0},{1},{2},{3},{4}",
values[0], values[1], values[2], values[3], values[4]));
} //exception being thrown here since the array is out of range
}
}
line.Split(',');but I don't see any commas it the 'actual csv file'?