user3206687 user3206687 - 1 year ago 115
C# Question

Split CSV files with header c#

I need to split large CSV files by the source field and name the export files the name as the source field.

My code works, but the only thing that's not working is I need the split files to have the header row from the original file.

Any help is appreciated. Thank you.

var splitQuery = from line in File.ReadLines(@"C:\test\test1.csv")
let source = line.Split(',').Last()
group line by source into outputs
select outputs;

foreach (var output in splitQuery)
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", output);

Im not sure how to add a snippet of the CSV but ive put a snippet of the header fields, hope this helps

ID ,Ref ,Title ,Initials ,Forename ,Surname ,File_Source

Answer Source

Simply read the header line first:

var fileLinesIterator = File.ReadLines(...);

string headerLine = fileLinesIterator.Take(1);

Then prepend it to every output:

var splitQuery = from line in fileLinesIterator

// ...

    File.WriteAllLines(@"C:\test\" + output.Key + ".csv", headerLine + "\r\n" + output);

But apart from that, you don't want to be handling CSV files as mere (lines of) strings. You're bound to running into running into trouble with quoted and multiline values.