Posted Post

Posted People, Posted Life.

ASP Function CSV2Array, VBScript CSV Format to Array

A useful function by ASP, VBScript, from CSV text file(string) to an Array data type.

vCSV: the string of CSV text
vDelimiter: the delimiter of CSV, normal use comma,
vQualifier: the qualifier, some CSV file use empty, others use “inverted comma” or “quotation marks”

Example:

CSV File:

Name,Age,Gender
Alen,25,Female
Alex,18,Female
Eric,30,Male

The Array you get will look like:

Array [

Name, Alen, Alex, Eric
Age, 25, 28, 30
Gender, Female, Female, Male

]

Array(0, 0) = “Name”
Array(0, 1) = “Alen”
Array(1, 0) =”Age”
Array(1, 1) =”25″
……

Function CSV2Array(vCSV, vDelimiter, vQualifier)

Dim vStr, vArray, vArray2, vLoop, vLoop2, vSpilt, X, Y, vResult
vStr = vCSV
If vDelimiter=”" Then vDelimiter = “,”
vSpilt = vDelimiter
If vQualifier<>”" Then

vStr = kLeachRegExp(vStr, “\” & vQualifier & ” *” & vDelimiter & ” *\” & vQualifier, vQualifier & vDelimiter & vQualifier)
vSpilt = vQualifier & vSpilt & vQualifier

End If
vStr = Replace(vStr, vbCr, vbLf)
vStr = Replace(vStr, vbLf & vbLf, vbLf)
vArray = Split(vStr, vbLf)
X = -1
Y = -1
For vLoop = 0 To UBound(vArray)

vStr = Trim(vArray(vLoop))
If vStr<>”" Then

Y = Y + 1
If Y = 0 Then

If inStr(vStr, vSpilt)>0 Then

X = UBound(Split(vStr, vSpilt))

Else

X = UBound(Split(vStr, vDelimiter))

End If

End If

End If

Next
ReDim vResult(X, Y)
If (X>=0) And (Y>=0) Then

Y = -1
For vLoop = 0 To UBound(vArray)

vStr = Trim(vArray(vLoop))
If vStr<>”" Then

Y = Y + 1

If vQualifier<>”" Then

If Left(vStr, 1) = vQualifier Then vStr = Mid(vStr, 2)

If Right(vStr, 1) = vQualifier Then vStr = Left(vStr, Len(vStr)-1)

End If
If inStr(vStr, vSpilt)>0 Then

vArray2 = Split(vStr, vSpilt)

Else

vArray2 = Split(vStr, vDelimiter)

End If
For vLoop2 = 0 To UBound(vArray2)

vResult(vLoop2, Y) = vArray2(vLoop2)

Next

End If

Next

End If
CSV2Array = vResult

End Function

The code, for your easy copy:

' ASP Function CSV2Array, VBScript CSV Format to Array
' http://postedpost.com/2008/08/13/asp-csv2array-function/
Function CSV2Array(vCSV, vDelimiter, vQualifier)
	Dim vStr, vArray, vArray2, vLoop, vLoop2, vSpilt, X, Y, vResult
	vStr = vCSV
	If vDelimiter="" Then vDelimiter = ","
	vSpilt = vDelimiter
	If vQualifier<>"" Then
		vStr = kLeachRegExp(vStr, "\" & vQualifier & " *" & vDelimiter & " *\" & vQualifier, vQualifier & vDelimiter & vQualifier)
		vSpilt = vQualifier & vSpilt & vQualifier
	End If
	vStr = Replace(vStr, vbCr, vbLf)
	vStr = Replace(vStr, vbLf & vbLf, vbLf)
	vArray = Split(vStr, vbLf)
	X = -1
	Y = -1
	For vLoop = 0 To UBound(vArray)
		vStr = Trim(vArray(vLoop))
		If vStr<>"" Then
			Y = Y + 1
			If Y = 0 Then
				If inStr(vStr, vSpilt)>0 Then
					X = UBound(Split(vStr, vSpilt))
				Else
					X = UBound(Split(vStr, vDelimiter))
				End If
			End If
		End If
	Next
	ReDim vResult(X, Y)
	If (X>=0) And (Y>=0) Then
		Y = -1
		For vLoop = 0 To UBound(vArray)
			vStr = Trim(vArray(vLoop))
			If vStr<>"" Then
				Y = Y + 1
				If vQualifier<>"" Then
					If Left(vStr, 1) = vQualifier Then vStr = Mid(vStr, 2)
					If Right(vStr, 1) = vQualifier Then vStr = Left(vStr, Len(vStr)-1)
				End If
				If inStr(vStr, vSpilt)>0 Then
					vArray2 = Split(vStr, vSpilt)
				Else
					vArray2 = Split(vStr, vDelimiter)
				End If
				For vLoop2 = 0 To UBound(vArray2)
					vResult(vLoop2, Y) = vArray2(vLoop2)
				Next
			End If
		Next
	End If
	CSV2Array = vResult
End Function

2 Comments »

  Farhana wrote @ September 21, 2008 at 7:33 am

Hi there,

thanks for sharing these information.
though, i have question to ask..

how if the user need to upload the csv file format into the system first?

only then the data will be extracted from the csv format to the database is it?

  Warren.rior wrote @ October 6, 2008 at 3:45 am

if you need upload file onto server, you need use 3party upload object.

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>