C # operations CSV file class instance

Source: Internet
Author: User

This example describes the C # operation CSV file class. Share to everyone for your reference. The specific analysis is as follows:

This C # class is used to convert a DataTable to a CSV file, CSV file to a DataTable, and this class is appropriate if you need to convert between CSV and DataTable.

Using system.data;using system.io;namespace dotnet.utilities{//<summary>///CSV file conversion class///</summary> public static class Csvhelper {//<summary>///Export Report as CSV//</summary>//<param name= "DT" >datat able</param>//<param name= "strFilePath" > Physical paths </param>//<param name= "TableHeader" > Headers < /param>//<param name= "columname" > field headings, comma delimited </param> public static bool Dt2csv (DataTable DT, String strf    Ilepath, String TableHeader, String columname) {try {string strbufferline = "";    StreamWriter strmwriterobj = new StreamWriter (strFilePath, False, System.Text.Encoding.UTF8);    Strmwriterobj.writeline (TableHeader);    Strmwriterobj.writeline (Columname); for (int i = 0; i < dt. Rows.Count;     i++) {strbufferline = ""; for (int j = 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 DataTable///</summary>//<param name= "filePath" >csv file path &LT;/PARAM&G  T <param name= "n" > indicates that the nth row is the field title, the N+1 line is the record start </param> public static DataTable Csv2dt (string filePath, int n,   DataTable dt) {StreamReader reader = new StreamReader (FilePath, System.Text.Encoding.UTF8, false);   int i = 0, m = 0; Reader.   Peek (); while (reader.    Peek () > 0) {m = m + 1; String str = 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);  }} return DT; } }}

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # operations CSV file class instance

This address: http://www.paobuke.com/develop/c-develop/pbk23064.html






Related content C # custom Event Properties Change raised event Example C # Video conversion class share detailed parsing C # Multithreading synchronization events and wait handles use examples of try and catch statements and finally statements in C # exception handling
C # readnodefile () Unable to read OSG file with file name Chinese character how to use GroupBy for grouping statistics in a DataTable WinForm implement method to crawl Web page content DevExpress Implement the method of setting watermark text for TextEdit

C # operations CSV file class instance

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.