|
-
Nov 1st, 2006, 09:55 AM
#1
Thread Starter
Addicted Member
[RESOLVED] write a 2d array to a .csv
OK, how would you write an array to a .csv?
e.g.
stored in Array(x,x) as string
Array(0,0) = 1
Array(0,1) = 12
Array(0,2) = 123
Array(1,0) = a
Array(1,1) = ab
Array(1,3) = abc
Output to .csv
1,12,123
a,ab,abc
-
Nov 1st, 2006, 12:54 PM
#2
Re: write a 2d array to a .csv
Something like this:
VB Code:
'a temp array (you can use your own array)
Dim arr(2, 2) As String
Dim i As Integer
Dim j As Integer
Dim strTemp As String
Dim strLine As String
Dim intFFN As Integer
arr(0, 0) = 1
arr(0, 1) = 12
arr(0, 2) = 123
arr(1, 0) = "a"
arr(1, 1) = "ab"
arr(1, 2) = "abc"
For i = 0 To UBound(arr, 1) - 1
For j = 0 To UBound(arr, 2)
If j = 0 Then
strLine = strLine & arr(i, j)
Else
strLine = strLine & "," & arr(i, j)
End If
Next
strTemp = strTemp & strLine & vbCrLf
strLine = ""
Next
intFFN = FreeFile
Open "c:\TempFile.csv" For Binary As intFFN
Put #intFFN, , strTemp
Close #intFFN
-
Nov 1st, 2006, 12:57 PM
#3
Re: write a 2d array to a .csv
repeated concatenation is slow, your better off doing the put statement in the loop.
-
Nov 1st, 2006, 01:03 PM
#4
Frenzied Member
Re: write a 2d array to a .csv
VB Code:
Sub SaveCSV(array() As String, path As String, Optional Delim As String = ",", Optional quote As String = "")
Dim opf As Long
Dim row As Long
Dim column As Long
If Dir(path) <> "" Then Kill path
opf = FreeFile
Open path For Binary As #opf
For row = LBound(array,1) To UBound(array,1)
For column = LBound(array,2) To UBound(array,2)
Put #opf, , quote & array(row,column) & quote
If column < UBound(array,2) Then Put #opf, , delim
Next column
If row < UBound(array,1) Then Put #opf, , vbCrLf
Next row
Close #opf
End Sub
Last edited by jeroen79; Nov 1st, 2006 at 01:12 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|