VB. net csv file processing class

Source: Internet
Author: User
Imports system. data. oledbimports system. dataimports system. textimports system. io ''' <summary> '''csv file read/write ''' </Summary> ''' <remarks> </remarks> Public notinheritable class CSV public shared Encoding As encoding = encoding. getencoding ("gb2312") Private shared connectionstring as string = "provider = Microsoft. jet. oledb.4.0; Data Source = {0}; extended properties = 'text; {1} '"public shared function opencsvfile (optional byval filter as string =" CSV (separated by commas )(*. CSV) | *. CSV ") as string dim path as string = string. empty using open as new openfiledialog open. multiselect = false open. filter = filter open. initialdirectory = application. startuppath if open. showdialog = 1 then Path = open. filename end using return path end function # region "read" ''' <summary> ''' reads CSV files ''' </Summary> ''' <Param name = "filepath "> CSV file path </param> ''' <Param name =" HDR "> whether a column title exists </param> ''' <returns> returned datatable </returns> ''' <remarks> </remarks> Public shared function read (byval filepath as string, optional byval HDR as Boolean = true) as datatable dim con as new oledbconnection (string. format (connectionstring, path. getdirectoryname (filepath), IIF (HDR, String. empty, "HDR = no;") dim name as string = path. getfilenamewithoutextension (filepath) dim dT as new datatable (name) using da as new oledbdataadapter (string. format ("select * from Hangzhou 02.16.csv]", name), con) Try da. fill (DT) catch ex as oledbexception msgbox (ex. message) end try end using con. dispose () return DT end function ''' <summary> ''' read CSV string ''' </Summary> ''' <Param name = "csvstring"> CSV format string </Param> ''' <returns> returned able </returns> ''' <remarks> </remarks> Public shared function stringread (byval csvstring as string) as datatable dim dT as new datatable on error resume next dim STR () as string = Split (csvstring, vbcrlf) dim CS () as string = Split (STR (0 ),", "c) for I = 0 to CS. length-1 DT. columns. add (CS (I), GetType (string) Next for I = 1 to Str. length-1 dim cells () as string = Split (STR (I), "," C) If cells. length <> DT. columns. count then continue for else DT. rows. add (split (STR (I), "," c) end if next 'dt. primarykey = new datacolumn () {DT. columns (0 )} return DT end function ''' <summary> ''' read CSV file ''' </Summary> ''' <Param name = "filepath"> CSV file path </Param> ''' <returns> returned able </returns> ''' <remarks> </remarks> Public shared function read2 (byval filepath as string) as datatable using SR as new streamreader (filepath, encoding) return stringread (Sr. readtoend) Sr. close () end using end function # End Region # region "write" ''' <summary> ''' Save the able data to the CSV file ''' </Summary> ''' <Param name = "path"> CSV file path </param> ''' <Param name = "table"> datatable to be saved </param> ''' <remarks> </remarks> public shared sub save (byval path as string, byval table as datatable) using SW as new streamwriter (path, false, encoding) SW. write (csvstring (table): Sw. flush (): Sw. close () end using end sub ''' <summary> ''' Save the datarow array to the CSV file ''' </Summary> ''' <Param name = "path"> CSV file path </param> ''' <Param name = "rows"> datarow array to be saved </param> ''' <remarks> </remarks> Public shared sub save (byval path as string, byval rows () as datarow) using SW as new streamwriter (path, false, encoding) SW. write (csvstring (rows): Sw. flush (): Sw. close () end using end sub ''' <summary> ''' Save the string to the CSV file ''' </Summary> ''' <Param name = "path"> CSV file path </param> ''' <Param name = "S"> to save the string </param> ''' <remarks> </remarks> Public shared sub save (byval path as string, byval s as string) using SW as new streamwriter (path, false, encoding) SW. write (s): Sw. flush (): Sw. close () End using end sub # End Region # region "utils" private shared function csvstring (byval table as datatable) as stringbuilder dim STR as new stringbuilder if table. columns. count = 0 then return STR for each C as datacolumn in table. columns Str. append (cellstring (C. columnname) & ",") Next Str. remove (Str. length-1, 1 ). append (vbcrlf) for each R as datarow in table. rows Str. appendline (rowstring (R) next return STR end function private shared function csvstring (byval rows () as datarow) as stringbuilder dim STR as new stringbuilder for each R as datarow in rows Str. appendline (rowstring (R) next return STR end function private shared function rowstring (byval row as datarow) as string dim STR as new stringbuilder for each s in row. itemarray Str. append (cellstring (S. tostring) & ",") next return Str. remove (Str. length-1, 1 ). tostring end function ''' <summary> ''' CSV String Conversion ''' </Summary> ''' <Param name = "S"> string to be converted </Param> ''' <returns> returns the converted string </returns> ''' <remarks> </remarks> Public shared function cellstring (byval s as string) as string if string. isnullorwhitespace (s) then return string. empty dim STR as new stringbuilder (s) if isaddchr34 (s) Then Str. replace (CHR (34), CHR (34) & CHR (34) Str. insert (0, CHR (34) Str. append (CHR (34) end if return Str. tostring end function private shared function isaddchr34 (byval s as string) as Boolean return S. indexof (CHR (10) <>-1 orelse S. indexof (CHR (13) <>-1 _ orelse S. indexof (CHR (34) <>-1 orelse S. indexof (CHR (44) <>-1 end function # End regionend class

Http://knab.ws/blog/index.php? /Archives/10-csv-file-parser-and-writer-in-c-part-2.html

Http://blog.bossma.cn/csharp/a-fast-csv-reader-tool-user-csharp/

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.