认真工作,天天向上

不断的学习,不断的总结

导航

CSV文件解析类

Imports System
Imports System.Text
Imports System.Collections.Specialized
Imports System.Collections
Imports System.IO


Namespace Utility

    
Public Class CSV

        
'class变量
        Private mCSVData As Object
        
Private mCSVFilePath As String
        
Private mCSVDataType As EDATATYPE
        
Private mwithColumeName As Boolean = True
        
Private mColumesNumber As Integer = -1
        
Private mRowsNumber As Integer = -1

        
Private myRegex As System.Text.RegularExpressions.Regex = _
              
New System.Text.RegularExpressions.Regex(",(?=([^']*'[^']*')*(?![^']*'))")         '

        
Public Sub New()

        
End Sub

 
        
Public Sub New(ByVal CSVFilePath As String)
            mCSVFilePath 
= CSVFilePath
        
End Sub


        
Public Sub New(ByVal CSVFilePath As StringByVal CSVData As ObjectByVal DataType As EDATATYPE)
            mCSVFilePath 
= CSVFilePath
            mCSVData 
= CSVData
            mCSVDataType 
= DataType
        
End Sub



        
Public Property CSVFilePath() As String
            
Get
                
Return mCSVFilePath
            
End Get
            
Set(ByVal Value As String)
                mCSVFilePath 
= Value
            
End Set
        
End Property



        
Public Property CSVData() As Object
            
Get
                
Return mCSVData
            
End Get
            
Set(ByVal Value As Object)
                mCSVData 
= Value
            
End Set
        
End Property



        
Public Property WithColumeName() As Boolean
            
Get
                
Return mwithColumeName
            
End Get
            
Set(ByVal Value As Boolean)
                mwithColumeName 
= Value
            
End Set
        
End Property



        
Public Property RowsNumber() As Integer
            
Get
                
Return mRowsNumber
            
End Get
            
Set(ByVal Value As Integer)
                mRowsNumber 
= Value
            
End Set
        
End Property


        
Public Property ColumesNumber() As Integer
            
Get
                
Return mColumesNumber
            
End Get
            
Set(ByVal Value As Integer)
                mColumesNumber 
= Value
            
End Set
        
End Property


        
Public Function Parse() As ArrayList
            
Return Parse(mCSVFilePath, Encoding.GetEncoding("Shift-JIS"))
        
End Function


        
Public Function Parse(ByVal FileName As StringAs ArrayList
            
Return Parse(FileName, Encoding.GetEncoding("Shift-JIS"))
        
End Function


               
Public Function Parse(ByVal FileName As StringByVal encoding As Encoding) As ArrayList
            
Dim txt As String
            
Dim reader As StreamReader
            mCSVData 
= New ArrayList

            
Try
                reader 
= New StreamReader(FileName, encoding)
                
Do While reader.Peek >= 0

                    
Dim s As String
                    s 
= reader.ReadLine()
                    
Dim myResult As String() = myRegex.Split(s)
                    mCSVData.Add(myResult)
                    
' If s = Nothing Then Exit Do
                Loop
            
Catch e As IOException
                
Throw e
            
Catch e As Exception
            
Finally
                
If Not reader Is Nothing Then
                    reader.Close()
                
End If
            
End Try
            
Return mCSVData
        
End Function


        
Public Function ParseString(ByVal strAs As StringAs ArrayList
            
Dim txt As String
            
Dim reader As StreamReader
            
Dim strRow() As String
            mCSVData 
= New ArrayList
            
Try
                strRow 
= strAs.Split(Char.Parse(vbLf))
                
Dim i As Integer = 0
                
Do
                    
Dim s As String
                    s 
= strRow(i)
                    i 
+= 1
                    
Dim myResult As String() = myRegex.Split(s)
                    mCSVData.Add(myResult)
                    
If s = Nothing Then Exit Do
                
Loop
            
Catch e As IOException
                
Throw e
            
Catch e As Exception
            
Finally
            
End Try
            
Return mCSVData
        
End Function



                
Public Function Creat() As Boolean
            
Dim i As Integer
            
Dim n As Integer
            
Dim strTemp As String

            
Dim Writer As New StreamWriter(mCSVFilePath, False, Encoding.GetEncoding("Shift-JIS"))
            
'mCSVData
            Try
                
Select Case mCSVDataType

                    
'datatable
                Case EDATATYPE.DATATABLE
                        
Dim intColumesMin As Integer
                        
Dim intRowsMin As Integer


                        
Dim arrData As DataTable
                        arrData 
= mCSVData
                        
'set the ColumesNumber
                        If mColumesNumber = -1 Then
                            intColumesMin 
= arrData.Columns.Count
                        
Else
                            intColumesMin 
= Math.Min(mColumesNumber, arrData.Columns.Count)
                        
End If
                        
'set the RowsNumber
                        If intRowsMin = -1 Then
                            intRowsMin 
= arrData.Rows.Count
                        
Else
                            intRowsMin 
= Math.Min(mRowsNumber, arrData.Rows.Count)
                        
End If
                        
'if has the columename then write the columename first
                        If mwithColumeName Then
                            
For i = 0 To intColumesMin - 1
                                strTemp 
= strTemp & arrData.Columns(i).ColumnName & ","
                            
Next
                            
If strTemp.Trim.EndsWith(","Then
                                strTemp 
= strTemp.Trim.Remove(strTemp.Trim.Length - 11)
                            
End If
                            Writer.WriteLine(strTemp)
                        
End If

                        
'Generate the data one by one
                        For i = 0 To intRowsMin - 1
                            strTemp 
= ""
                            
For n = 0 To intColumesMin - 1
                                strTemp 
= strTemp & arrData.Rows(i).Item(n) & ","
                            
Next
                            
If strTemp.Trim.EndsWith(","Then
                                strTemp 
= strTemp.Trim.Remove(strTemp.Trim.Length - 11)

                            
End If
                            Writer.WriteLine(strTemp)
 
                            Debug.WriteLine(
"Creat line " & (i + 1& " :" & strTemp)

                        
Next
                        
'arraylist
                    Case EDATATYPE.ARRAYLIST
                        
Dim arrData As ArrayList
                        
Dim arrTemp As ArrayList

                        arrData 
= mCSVData
                        
For i = 0 To arrData.Count - 1
                            arrTemp 
= arrData(i)
                            strTemp 
= ""
                            
For n = 0 To arrTemp.Count - 1
                                strTemp 
= arrTemp(n) & ","
                            
Next
                            Writer.WriteLine(strTemp)
                        
Next

                
End Select
                Writer.Close()

                
Return True
            
Catch ex As Exception
                Debug.WriteLine(ex.ToString)
                
Return False
            
Finally

            
End Try

        
End Function


    
End Class

End Namespace

posted on 2007-01-05 17:32  MYOOP  阅读(577)  评论(0编辑  收藏  举报