usingSystem.Data;usingSystem.IO;namespacedotnet.utilities{/// <summary> ///CSV file Conversion class/// </summary> Public Static classCsvhelper {/// <summary> ///Export Report as CSV/// </summary> /// <param name= "DT" >DataTable</param> /// <param name= "strFilePath" >Physical Path</param> /// <param name= "TableHeader" >Table Header</param> /// <param name= "Columname" >field headings, comma delimited</param> Public Static BOOLDt2csv (DataTable DT,stringstrFilePath,stringTableHeader,stringcolumname) { Try { stringStrbufferline =""; StreamWriter Strmwriterobj=NewStreamWriter (strFilePath,false, System.Text.Encoding.UTF8); Strmwriterobj.writeline (TableHeader); Strmwriterobj.writeline (Columname); for(inti =0; i < dt. Rows.Count; i++) {Strbufferline=""; for(intj =0; J < dt. Columns.count; J + +) { if(J >0) Strbufferline+=","; Strbufferline+=dt. ROWS[I][J]. ToString (); } strmwriterobj.writeline (Strbufferline); } strmwriterobj.close (); return true; } Catch { return false; } } /// <summary> ///read CSV into a DataTable/// </summary> /// <param name= "FilePath" >csv file path</param> /// <param name= "n" >Indicates that the nth row is the field title, and the first line of N+1 is the beginning of the record</param> Public StaticDataTable Csv2dt (stringFilePath,intN, DataTable DT) {StreamReader Reader=NewStreamReader (FilePath, System.Text.Encoding.UTF8,false); inti =0, M =0; Reader. Peek (); while(Reader. Peek () >0) {m= m +1; stringstr =Reader. ReadLine (); if(M >= n +1) { string[] split = str. Split (','); System.Data.DataRow Dr=dt. NewRow (); for(i =0; I < split. Length; i++) {Dr[i]=Split[i]; } dt. Rows.Add (DR); } } returnDT; } }}//The code snippet comes from:http://www.sharejs.com/codes/csharp/8609
A packaged CSV file to manipulate C # class code