Thursday, 15 July 2010

c# - skiping first line when writing to csv file -



c# - skiping first line when writing to csv file -

i have wrote code read , write csv file. there way skip writing first row headers , want write own headers new file. have done far

seek { using (streamwriter sw = new streamwriter("c:/projects/data/pyaegon1aegonresult.csv")) using (streamreader sr = new streamreader("c:/projects/data/pyaegon1aegon.csv")) { heading = "title, first name, surname, date of birth, ni number, gender, payroll reference, address line 1, address line 2, address line 3, address line 4, addressline 5, postcode, country, email, date joined employer, annual pensionable salary, current period earnings (monthly), current period pensionable earnings (monthly), emnployee (amount deducted), employer (amount deducted), leaving date."; sw.writeline(heading); while((txtline = sr.readline()) != null) { oldcolumns = regex.split(txtline,","); newcolumns[0] = oldcolumns[0]; newcolumns[1] = oldcolumns[1]; newcolumns[2] = oldcolumns[2]; newcolumns[3] = oldcolumns[3]; newcolumns[4] = oldcolumns[4]; newcolumns[5] = oldcolumns[5]; newcolumns[6] = oldcolumns[6]; newcolumns[7] = oldcolumns[7]; newcolumns[8] = oldcolumns[9]; newcolumns[9] = oldcolumns[10]; newcolumns[10] = oldcolumns[11]; newcolumns[11] = ""; newcolumns[12] = oldcolumns[12]; newcolumns[13] = "united kingdom"; newcolumns[14] = oldcolumns[14]; newcolumns[15] = oldcolumns[15]; newcolumns[16] = oldcolumns[16]; newcolumns[17] = oldcolumns[17]; newcolumns[18] = oldcolumns[18]; newcolumns[19] = oldcolumns[19]; newcolumns[20] = oldcolumns[20]; newcolumns[21] = ""; csvline = ""; (int = 0; < 21; i++) { csvline = csvline + "\"" + newcolumns[i].replace("\"","") + "\","; } sw.writeline(csvline); }

just phone call sr.readline() before start reading input.

using (streamwriter sw = new streamwriter("c:/projects/data/pyaegon1aegonresult.csv")) using (streamreader sr = new streamreader("c:/projects/data/pyaegon1aegon.csv")) { sr.readline(); // rest of code

c# csv

No comments:

Post a Comment