Imports System Imports System.Text Imports System.Collections.Specialized Imports System.Collections Imports System.IO Namespace UtilityNamespace Utility PublicClass CSVClass CSV 'class变量 Private mCSVData AsObject Private mCSVFilePath AsString Private mCSVDataType As EDATATYPE Private mwithColumeName AsBoolean=True Private mColumesNumber AsInteger=-1 Private mRowsNumber AsInteger=-1 Private myRegex As System.Text.RegularExpressions.Regex = _ New System.Text.RegularExpressions.Regex(",(?=([^']*'[^']*')*(?![^']*'))") ' PublicSub New()SubNew() End Sub PublicSub New()SubNew(ByVal CSVFilePath AsString) mCSVFilePath = CSVFilePath End Sub PublicSub New()SubNew(ByVal CSVFilePath AsString, ByVal CSVData AsObject, ByVal DataType As EDATATYPE) mCSVFilePath = CSVFilePath mCSVData = CSVData mCSVDataType = DataType End Sub PublicProperty CSVFilePath()Property CSVFilePath() AsString Get Return mCSVFilePath EndGet Set(ByVal Value AsString) mCSVFilePath = Value EndSet End Property PublicProperty CSVData()Property CSVData() AsObject Get Return mCSVData EndGet Set(ByVal Value AsObject) mCSVData = Value EndSet End Property PublicProperty WithColumeName()Property WithColumeName() AsBoolean Get Return mwithColumeName EndGet Set(ByVal Value AsBoolean) mwithColumeName = Value EndSet End Property PublicProperty RowsNumber()Property RowsNumber() AsInteger Get Return mRowsNumber EndGet Set(ByVal Value AsInteger) mRowsNumber = Value EndSet End Property PublicProperty ColumesNumber()Property ColumesNumber() AsInteger Get Return mColumesNumber EndGet Set(ByVal Value AsInteger) mColumesNumber = Value EndSet End Property PublicFunction Parse()Function Parse() As ArrayList Return Parse(mCSVFilePath, Encoding.GetEncoding("Shift-JIS")) End Function PublicFunction Parse()Function Parse(ByVal FileName AsString) As ArrayList Return Parse(FileName, Encoding.GetEncoding("Shift-JIS")) End Function PublicFunction Parse()Function Parse(ByVal FileName AsString, ByVal encoding As Encoding) As ArrayList Dim txt AsString Dim reader As StreamReader mCSVData =New ArrayList Try reader =New StreamReader(FileName, encoding) DoWhile reader.Peek >=0 Dim s AsString s = reader.ReadLine() Dim myResult AsString() = myRegex.Split(s) mCSVData.Add(myResult) ' If s = Nothing Then Exit Do Loop Catch e As IOException Throw e Catch e As Exception Finally IfNot reader IsNothingThen reader.Close() EndIf EndTry Return mCSVData End Function PublicFunction ParseString()Function ParseString(ByVal strAs AsString) As ArrayList Dim txt AsString Dim reader As StreamReader Dim strRow() AsString mCSVData =New ArrayList Try strRow = strAs.Split(Char.Parse(vbLf)) Dim i AsInteger=0 Do Dim s AsString s = strRow(i) i +=1 Dim myResult AsString() = myRegex.Split(s) mCSVData.Add(myResult) If s =NothingThenExitDo Loop Catch e As IOException Throw e Catch e As Exception Finally EndTry Return mCSVData End Function PublicFunction Creat()Function Creat() AsBoolean Dim i AsInteger Dim n AsInteger Dim strTemp AsString Dim Writer AsNew StreamWriter(mCSVFilePath, False, Encoding.GetEncoding("Shift-JIS")) 'mCSVData Try SelectCase mCSVDataType 'datatable Case EDATATYPE.DATATABLE Dim intColumesMin AsInteger Dim intRowsMin AsInteger Dim arrData As DataTable arrData = mCSVData 'set the ColumesNumber If mColumesNumber =-1Then intColumesMin = arrData.Columns.Count Else intColumesMin = Math.Min(mColumesNumber, arrData.Columns.Count) EndIf 'set the RowsNumber If intRowsMin =-1Then intRowsMin = arrData.Rows.Count Else intRowsMin = Math.Min(mRowsNumber, arrData.Rows.Count) EndIf 'if has the columename then write the columename first If mwithColumeName Then For i =0To intColumesMin -1 strTemp = strTemp & arrData.Columns(i).ColumnName &"," Next If strTemp.Trim.EndsWith(",") Then strTemp = strTemp.Trim.Remove(strTemp.Trim.Length -1, 1) EndIf Writer.WriteLine(strTemp) EndIf 'Generate the data one by one For i =0To intRowsMin -1 strTemp ="" For n =0To intColumesMin -1 strTemp = strTemp & arrData.Rows(i).Item(n) &"," Next If strTemp.Trim.EndsWith(",") Then strTemp = strTemp.Trim.Remove(strTemp.Trim.Length -1, 1) EndIf 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 =0To arrData.Count -1 arrTemp = arrData(i) strTemp ="" For n =0To arrTemp.Count -1 strTemp = arrTemp(n) &"," Next Writer.WriteLine(strTemp) Next EndSelect Writer.Close() ReturnTrue Catch ex As Exception Debug.WriteLine(ex.ToString) ReturnFalse Finally EndTry End Function End Class End Namespace
posted on
2007-01-05 17:32MYOOP
阅读(577)
评论(0)
编辑收藏举报