I am trying to read an uploaded CSV file and before doing anything with the data I need to check the first header name to be sure it is the correct file. I have been trying to find a way to do it but the reader skips to the second row instead. Is there a direct way of selecting one of the headers and checking its value?
You can use the CsvReader to get the header row strings as described in this answer:
using (var csv = new CsvReader(reader))
{
csv.Read();
csv.ReadHeader();
string[] headerRow = csv.Context.HeaderRecord;
}
You can use the parser directly if you want to just check the first row.
var parser = new CsvParser( textReader );
var row = parser.Read();
if( row[0] == "MyColumn" ) { /* do something */ }
If you're using a Stream
, you will need to reset it to the beginning if you're going to use it again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With