Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Const delim = ","        ' file:field seperator
  5. Public Const pad_string = "."   ' pad string for display
  6.  
  7. Public Sub ReadCSV()
  8.     ' variables for file handling
  9.     Dim file_name As String
  10.     Dim fnum As Integer         'fHandle
  11.     Dim whole_file As String
  12.     Dim lines As Variant        ' UBound !
  13.     Dim one_line As Variant     ' UBound !
  14.     ' to store the result here
  15.     Dim num_rows As Long
  16.     Dim num_cols As Long
  17.     Dim my_array() As String
  18.     Dim R As Long               ' rows
  19.     Dim C As Long               ' colums
  20.     ' for display in sheet style
  21.     Dim cols_width() As Integer ' width of each column
  22.     Dim sheetwidth As Integer
  23.     Dim line_out                ' formated string for display
  24.    
  25.     ' the file is in the same directory as the db
  26.     file_name = Application.CurrentProject.Path
  27.     If Right$(file_name, 1) <> "\" Then file_name = _
  28.         file_name & "\"
  29.     file_name = file_name & "daten.csv"
  30.  
  31.     ' Load the complete file.
  32.     fnum = FreeFile
  33.     Open file_name For Input As fnum
  34.     whole_file = Input$(LOF(fnum), #fnum)
  35.     Close fnum
  36.  
  37.     ' Break the file into lines.
  38.     lines = Split(whole_file, vbCrLf)
  39.  
  40.     ' Dimension the array.
  41.     num_rows = UBound(lines)
  42.     one_line = Split(lines(0), delim)
  43.     num_cols = UBound(one_line)
  44.     ReDim my_array(num_rows, num_cols)
  45.  
  46.     ' Copy the data into the array.
  47.     For R = 0 To num_rows
  48.         If Len(lines(R)) > 0 Then
  49.             one_line = Split(lines(R), delim)
  50.             For C = 0 To num_cols
  51.                 my_array(R, C) = one_line(C)
  52.             Next C
  53.         End If
  54.     Next R
  55.    
  56.     ' *********** PREPARE FOR DISPLAY **********
  57.    
  58.     ' ** calc the spreadsheed column width **
  59.     'init array
  60.     ReDim cols_width(num_cols)
  61.     ' calc the max column width per column
  62.     For C = 0 To num_cols
  63.         cols_width(C) = 0
  64.     Next C
  65.     For R = 0 To num_rows
  66.         For C = 0 To num_cols
  67.             If Len(my_array(R, C)) > cols_width(C) Then _
  68.             cols_width(C) = Len(my_array(R, C))
  69.         Next C
  70.     Next R
  71.    
  72.     'calc sheetwidth for the underline of the headline
  73.     sheetwidth = 0
  74.     For C = 0 To num_cols
  75.         sheetwidth = sheetwidth + cols_width(C)
  76.     Next C
  77.    
  78.     ' Print the Data
  79.     For R = 0 To num_rows
  80.         line_out = ""       ' clear line
  81.         For C = 0 To num_cols
  82.             'Debug.Print my_array(R, C);
  83.             line_out = line_out & my_array(R, C) & String(cols_width(C) - Len(my_array(R, C)), pad_string)
  84.             If C < num_cols Then line_out = line_out & "|" ' Debug.Print "|";
  85.             ' underline headline
  86.             If R = 0 And C = num_cols Then line_out = line_out & vbCrLf _
  87.                 & String(sheetwidth + num_cols, "=")
  88.         Next C
  89.         Debug.Print line_out
  90.     Next R
  91.    
  92. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement