A packaged CSV file to manipulate C # class code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.