Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Compare Database
- Option Explicit
- Public Const delim = "," ' file:field seperator
- Public Const pad_string = "." ' pad string for display
- Public Sub ReadCSV()
- ' variables for file handling
- Dim file_name As String
- Dim fnum As Integer 'fHandle
- Dim whole_file As String
- Dim lines As Variant ' UBound !
- Dim one_line As Variant ' UBound !
- ' to store the result here
- Dim num_rows As Long
- Dim num_cols As Long
- Dim my_array() As String
- Dim R As Long ' rows
- Dim C As Long ' colums
- ' for display in sheet style
- Dim cols_width() As Integer ' width of each column
- Dim sheetwidth As Integer
- Dim line_out ' formated string for display
- ' the file is in the same directory as the db
- file_name = Application.CurrentProject.Path
- If Right$(file_name, 1) <> "\" Then file_name = _
- file_name & "\"
- file_name = file_name & "daten.csv"
- ' Load the complete file.
- fnum = FreeFile
- Open file_name For Input As fnum
- whole_file = Input$(LOF(fnum), #fnum)
- Close fnum
- ' Break the file into lines.
- lines = Split(whole_file, vbCrLf)
- ' Dimension the array.
- num_rows = UBound(lines)
- one_line = Split(lines(0), delim)
- num_cols = UBound(one_line)
- ReDim my_array(num_rows, num_cols)
- ' Copy the data into the array.
- For R = 0 To num_rows
- If Len(lines(R)) > 0 Then
- one_line = Split(lines(R), delim)
- For C = 0 To num_cols
- my_array(R, C) = one_line(C)
- Next C
- End If
- Next R
- ' *********** PREPARE FOR DISPLAY **********
- ' ** calc the spreadsheed column width **
- 'init array
- ReDim cols_width(num_cols)
- ' calc the max column width per column
- For C = 0 To num_cols
- cols_width(C) = 0
- Next C
- For R = 0 To num_rows
- For C = 0 To num_cols
- If Len(my_array(R, C)) > cols_width(C) Then _
- cols_width(C) = Len(my_array(R, C))
- Next C
- Next R
- 'calc sheetwidth for the underline of the headline
- sheetwidth = 0
- For C = 0 To num_cols
- sheetwidth = sheetwidth + cols_width(C)
- Next C
- ' Print the Data
- For R = 0 To num_rows
- line_out = "" ' clear line
- For C = 0 To num_cols
- 'Debug.Print my_array(R, C);
- line_out = line_out & my_array(R, C) & String(cols_width(C) - Len(my_array(R, C)), pad_string)
- If C < num_cols Then line_out = line_out & "|" ' Debug.Print "|";
- ' underline headline
- If R = 0 And C = num_cols Then line_out = line_out & vbCrLf _
- & String(sheetwidth + num_cols, "=")
- Next C
- Debug.Print line_out
- Next R
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement