Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
.NET CSV Parser
Author:
unkarae
E-mail:
Click to e-mail author
Submitted:
11/6/2005
Version:
VB.NET 2003
Compatibility:
VB.NET 2003, ASP.NET 1.1
Category:
ASP.NET
Views:
69837
Code for CSV Parsing in .NET.
Declarations:
Imports System Imports System.IO
Code:
Module modParseCSV Public Sub ReadFromFile(ByVal strPath As String) Dim FileHolder As FileInfo = New FileInfo(strPath) Dim ReadFile As StreamReader = FileHolder.OpenText() Dim InputText As String Dim strLine Do strLine = ReadFile.ReadLine If Not strLine = "" Then ParseCSV(strLine) MsgBox("ENDLINE") Else ReadFile.Close() ReadFile = Nothing End If Loop Until ReadFile Is Nothing Return True End sub Public Sub ParseCSV(ByVal CSVstr As String) Dim startPos As Integer Dim endPos As Integer Dim currPos As Integer Dim tempPos As Integer Dim tempstr As String Dim commaPos As Integer Dim quotePos As Integer Dim strLen As Integer Dim charLen As Integer startPos = 1 currPos = 1 strLen = Len(CSVstr) Do While strLen <> 0 CSVstr = Replace(CSVstr, Chr(34) & Chr(34), "'") commaPos = InStr(currPos, CSVstr, ",") quotePos = InStr(currPos, CSVstr, Chr(34)) 'last data If commaPos = 0 Then If quotePos = 0 Then If Not currPos > endPos Then endPos = strLen + 1 charLen = endPos - currPos tempstr = Mid(CSVstr, currPos, charLen) 'If Not tempstr = "" Then ReadChars(tempstr, 1, charLen, charLen) 'End If End If Else currPos = quotePos endPos = InStr(currPos + 1, CSVstr, Chr(34)) charLen = endPos - currPos tempstr = Mid(CSVstr, currPos + 1, charLen - 1) 'If Not tempstr = "" Then ReadChars(tempstr, 1, charLen, charLen) 'End If End If Exit Do End If 'no " in line If quotePos = 0 Then endPos = commaPos charLen = endPos - currPos tempstr = Mid(CSVstr, currPos, charLen) 'If Not tempstr = "" Then ReadChars(tempstr, 1, charLen, charLen) 'End If ElseIf (quotePos <> 0) Then '" in line If commaPos < quotePos Then endPos = commaPos charLen = endPos - currPos tempstr = Mid(CSVstr, currPos, charLen) 'If Not tempstr = "" Then ReadChars(tempstr, 1, charLen, charLen) 'End If Else currPos = quotePos endPos = InStr(currPos + 1, CSVstr, Chr(34)) charLen = endPos - currPos tempstr = Mid(CSVstr, currPos + 1, charLen - 1) 'If Not tempstr = "" Then ReadChars(tempstr, 1, charLen, charLen) 'End If endPos = endPos + 1 End If End If currPos = endPos + 1 Loop End sub Public Function ReadChars(ByVal str As String, ByVal StartPos As Integer, ByVal EndPos As Integer, ByVal strLen As Integer) Dim strArray As [String] = str Dim b(strLen) As Char Dim sr As New StringReader(strArray) sr.Read(b, 0, EndPos) MsgBox(b) sr.Close() End Function End Module
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement