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 </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