Dim fuu() As String Dim j, i As Integer fuu = System.IO.File.ReadAllLines(fichiertemp)'on fait un array de string de façon à pouvoir traiter chaque ligne facilement i = 1 j = 1 For Each ligne In fuu 'on traite ligne par ligne dans le array xlBook.ActiveSheet.Cells(i, j).Value = ligne If j = 4 Then 'on arrive au bout de la ligne donc on le reinitialise à 1 et on passe à la ligne suivante j = 1 i = i + 1 Else j = j + 1 End If Next
Dim fichiertexte As String = "C:\..." 'chemin complet non affiché pour cet exemple fichiertexte = fichiertexte + DataGridView1.CurrentCell.Value fichiertexte = fichiertexte + ".txt" Dim fichiertemp As String = "C:\...\fichiertemp.txt" 'chemin complet non affiché pour cet exemple Using MyReader As New _ Microsoft.VisualBasic.FileIO.TextFieldParser(fichiertexte) MyReader.TextFieldType = FileIO.FieldType.Delimited Dim sw As StreamWriter = System.IO.File.AppendText(fichiertemp) MyReader.SetDelimiters(" ") Dim currentRow As String() While Not MyReader.EndOfData currentRow = MyReader.ReadFields() Dim currentField As String For Each currentField In currentRow If (currentField <> (" ")) Then If (currentField <> ("")) Then sw.WriteLine(currentField) 'ecrire directement dans la cellule excel (maintenant possible) End If End If Next End While End Using Dim xlApp As New Excel.Application Dim xlBook As Workbook Dim xlsfeuille As Excel.Worksheet Dim Nomxls As String xlApp = CreateObject("Excel.Application") xlBook = xlApp.Workbooks.Add xlsfeuille = xlBook.Worksheets(1) Nomxls = "C:\..." 'chemin complet non affiché pour cet exemple Nomxls = Nomxls + DataGridView1.CurrentRow.Cells(1).Value.ToString() Nomxls = Nomxls + ".xlsx" Dim filesys, readfile, contents Dim j, i As Integer filesys = CreateObject("Scripting.FileSystemObject") readfile = filesys.OpenTextFile(fichiertemp, 1, False) i = 1 Do While readfile.AtEndOfStream = False For j 1 To j 4 Step 1 contents = readfile.ReadLine xlsfeuille.Cells(i, j).Value = contents Next j i = i + 1 Loop readfile.close()
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question