c# CSV file prepend header row

  • Thread starter Thread starter G-Oker
  • Start date Start date
G

G-Oker

Guest
hello,


Ive got a c# program that creates a CSV from the output of a SQL query (using streamWriter to wirter.Writeline( "list, of, my, headers"), but fursly, it adds a header row, and this all works fine :-) .

But now, I have a new field added to the output (column 11) that the CSV need to be ordered by.

I have resolved this part by using

string[] lines = System.IO.File.ReadAllLines(FullPathToMyResultsFile);

IEnumerable<string> query =
from line in lines
let x = line.Split(',')
orderby x[10]
select x[0] +","+ x[1] + "," + x[2] + "," +x[3] + "," +x[4] + "," + x[5] + "," + x[6] + "," + x[7] + "," + x[8] + "," + x[9] + "," +x[10];
System.IO.File.WriteAllLines(FullPathToMyuResultsFile, query.ToArray());


but this takes into account the header row, so it ends up at the bottom of the CSV. :-(

I can initially NOT add the header row, but how do I then insert it at the beginning of the csv file once I have reordered it ?, or tell IEnumberable to ignore the first line, but add it back in as the first line once the data has been ordered ?


Many thanks in advance

Continue reading...
 
Back
Top