Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

Copiare da file .txt a .xlsx

$
0
0

Buona sera avrei la necessità di copiare delle righe da un file di testo ad una colonna A di un file excel!

Prima di copiarle però dovrei verificare se sono già presenti!

Per fare delle prove ho copiato queste righe in un altro file di testo e funziona!

Vi posto il codice

Private Sub btnConfronta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfronta.Click

  Dim FILE_NAME As String = TextBox1.Text

        Dim FILE_NAME2 As String = TextBox2.Text

        If File.Exists(FILE_NAME) Then

            If File.Exists(FILE_NAME2) Then

                Dim readText() As String = File.ReadAllLines(FILE_NAME)

                Dim sr As StreamWriter = File.CreateText(FILE_NAME2)

                Dim stringa As String

                For Each stringa In readText

                    Dim indice1 As Integer, indice2 As Integer

                    Dim substring As String

                    indice1 = InStr(1, stringa, "[") + 1 

                    indice2 = InStr(indice1, stringa, "]") '

                    If indice1 <> 0 And indice2 <> 0 Then '

                        substring = Mid(stringa, indice1, indice2 - indice1) 

                        Console.WriteLine(substring)

                        sr.WriteLine(substring)

                    End If

                Next

                sr.Close()

                MessageBox.Show("File copiato correttamente")

            Else : MessageBox.Show("Il file '"& FILE_NAME2 & "' non esiste!")

            End If

        Else : MessageBox.Show("Il file '"& FILE_NAME & "' non esiste!")

        End If

Ora dovrei leggere il file excel e inserirgli le righe che non sono presenti!

Ho scritto questo codice ma mi sono bloccato!

Dim comm As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand("SELECT * FROM [PassiAOI$];")

        Dim excelLettura As System.Data.OleDb.OleDbDataReader

        Using conn As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='P:\users\simone\RACK x AOI.xlsx';Extended Properties=Excel 12.0;")

            Try

                conn.Open() 'apro la connessione ed eseguo la query

                comm.Connection = conn

                excelLettura = comm.ExecuteReader()

                Dim COD As String = ""

                While (excelLettura.Read()) 'per ogni record che trovo visualizzo a video il risultato

                    COD = excelLettura("COD").ToString()

                    Console.WriteLine(COD)

                    Application.DoEvents()

                End While

                excelLettura.Dispose()

                comm.Dispose()

            Catch ex As Exception

                MessageBox.Show("Errore: " + ex.Message)

            Finally

                If (conn.State = ConnectionState.Open) Then conn.Close() 'chiudo la connessione

            End Try

        End Using

Come posso procedere? 

 


Viewing all articles
Browse latest Browse all 2212

Trending Articles