c# - Skipping header when reading CSV -
i want able skip header when reading , writing csv
here code have far
try { using (streamwriter sw = new streamwriter(writefilename))// streamwriter used write text file using (streamreader sr = new streamreader(readfilename)) // streamreader used read text file { //writing headings file heading = "ni number,surname,forename,employee,employer(gross),contribution status,contribution status date"; sw.writeline(heading); while ((txtline = sr.readline()) != null) // reads 1 line variable txtline { //spiliting file columns if there in it. //oldcolumns = txtline.split(','); oldcolumns = regex.split(txtline, ","); //spliting old columns[0] there space , putting in names array. //names = regex.split(oldcolumns[0],","); string [] names = oldcolumns[0].split(' '); var result = string.join(" ", oldcolumns[0].split(' ').skip(1)); //writing oldcolumns data newcolumns. newcolumns[0] = oldcolumns[1]; newcolumns[1] = result; newcolumns[2] = names[0]; newcolumns[3] = oldcolumns[9]; newcolumns[4] = oldcolumns[11]; newcolumns[5] = ""; newcolumns[6] = ""; //using loop run through columns csvline = ""; (int = 0; < 7; i++) { csvline = csvline + "\"" + newcolumns[i].replace("\"", "") + "\","; } //writing file. sw.writeline(csvline); }
i understand if wanted skip column in loop dont know how skip row.
if want discard first line of file, call readline()
, discard result before further processing:
// discard header row sr.readline() // further processing while ((txtline = sr.readline()) != null) { }
please note 1 line not mean 1 row in csv. use library read csv.
Comments
Post a Comment