1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
Imports Microsoft.VisualBasic.FileIO
Imports System.Text
Public Class Form1
Private ArrFields(,) As String
Private fileNameToRead As String = Nothing
Private sb As StringBuilder
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
sb = New StringBuilder
fileNameToRead = ObtientNomFichier()
If Not String.IsNullOrEmpty(fileNameToRead) Then
ArrFields = GetListRows(fileNameToRead)
For irow As Integer = 0 To ArrFields.GetUpperBound(0)
For icol As Integer = 0 To ArrFields.GetUpperBound(1)
sb.Append(ArrFields(irow, icol).PadRight(5))
Next
sb.AppendLine()
Next
End If
TextBox1.Text = sb.ToString
End Sub
Private Function GetListRows(path As String) As String(,)
'open myreader to count lines and columns
Dim MyReader As New TextFieldParser(path)
MyReader.SetDelimiters(";")
Dim nbRow As Integer = MyReader.LineNumber - 1
Dim nbCol As Integer = MyReader.ReadFields().Length - 1
While Not MyReader.EndOfData
nbRow = MyReader.LineNumber
nbCol = MyReader.ReadFields().Length
End While
nbRow -= 1
nbCol -= 1
MyReader.Close()
Dim arrStr(nbRow, nbCol) As String
'reopen myreader to read lines
MyReader = New TextFieldParser(path)
MyReader.SetDelimiters(";")
Dim currentRow As String()
Dim i As Integer = 0
While Not MyReader.EndOfData
currentRow = MyReader.ReadFields()
For j As Integer = 0 To currentRow.Length - 1
arrStr(i, j) = currentRow(j)
Next
i += 1
End While
MyReader.Close()
Return arrStr
End Function
Private Function ObtientNomFichier() As String
Dim fileName As String = Nothing
OpenFileDialog1.Filter = "(files (*.csv)|*.csv"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
fileName = OpenFileDialog1.FileName
End If
Return fileName
End Function
End Class |
Partager