Import export CSV

Status
Niet open voor verdere reacties.

old Hippy

Gebruiker
Lid geworden
24 mei 2008
Berichten
911
Meestal zit het in een mailprogramma zelf. Ik heb hier enkele work arrounds voor je, maar weet niet wel mail programma je hebt en hier van toepassing is. Klikje. Wellicht zit er wel iets tussen voor je. :)
 
Beste manamana.
Helaas is het geen mail programma.
maar een import en export van een database.
Wat ik gebruiken wil als back-up en archiveren.
ik exporteer en importeer nu naar Excel dit geeft echter vaak problemen met getallen en soms ook met data.
Van daar deze poging met csv heb je daar geen probleem.
en ben dus al een tijd aan het zoeken dit te importeren en exporteren in een datatafel.

Het probleem zit in het export gedeelte de import werkt goed
 
Laatst bewerkt:
Ja natuurlijk ik dacht ook excel, sorry verkeerd gelezen. :confused:
 
Ok Geeft niets Even goed bedankt voor je reactie.

misschien weet iemand een goede export naar CSV.
Mag ook met datagrid
 
Hallo na veel gepruts de oplossing gevonden.

Voor de liefhebbers hier de oplossing
Vervang in de export het stukje code voor deze.
Code:
        While dr.Read()
            Dim strRow As String = ""
            Dim sDummy As String = ""
            For i As Decimal = 0 To dr.FieldCount - 1
                Try
                    ' if Setting value to the sDummy variable blows up, we go catch...
                    sDummy = dr.GetValue(i)
                    ' In case the value on the field has a comma (,), put a "" around the value...
                    strRow += If(sDummy.IndexOf(","), """"c & dr.GetValue(i) & """"c, dr.GetValue(i))
                    If i >= dr.FieldCount - 1 Then
                        Continue For
                    End If
                    strRow += separator
                Catch
                    If i < dr.FieldCount - 1 Then
                        strRow += separator
                    End If
                End Try
            Next
            sw.WriteLine(strRow)

        End While
 
Ik heb nog een probleempje
Heeft iemand een idee hoe ik dit proces met een progressbar kan maken.

Iets als dit dacht ik maar dat werkt niet .
Code:
 ProgressBarX1.Value = row.ToString / dt.Rows.Count.ToString * 100
 
Hoi,

HEt linkje uit de eerste post werkt niet.
Ik zoek ook een import en export vba functie.
Heb je toevallig iets wat je wilt delen?
 
Sorry voor het late antwoord vakantie.
heb echter niets voor vba
Groet Old Hippy
 
Misschien iets voor de liefhebber
de code voor de export in csv met progressbar

Vervang dit in de export code
Code:
   '
    '         * Asks a filename and location from the user to export the data, and
    '         * runs the export operation.
    Private Sub exportToCSV()
        ''Asks the filenam with a SaveFileDialog control.
        Dim saveFileDialogCSV As New SaveFileDialog()


        saveFileDialogCSV.InitialDirectory = Application.ExecutablePath.ToString()
        saveFileDialogCSV.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
        saveFileDialogCSV.FilterIndex = 1
        saveFileDialogCSV.RestoreDirectory = True
        Dim RowsCount As Integer = 0
        If saveFileDialogCSV.ShowDialog() = DialogResult.OK Then
            '' Runs the export operation if the given filenam is valid.
            exportToCSVfile(saveFileDialogCSV.FileName.ToString(), RowsCount)
        End If
       
    End Sub
    '
    '  * Exports data to the CSV file.
    '        

    Private Sub exportToCSVfile(fileOut As String, ByRef RowsCount As Integer)

        ' Connects to the database, and makes the select command.
        Dim conn As New SqlConnection(prop.sqlConnString)
        Dim sqlQuery As String = "select * from " + Me.lbxTables.SelectedItem.ToString()
        Dim command As New SqlCommand(sqlQuery, conn)
        conn.Open()
        ' Creates a SqlDataReader instance to read data from the table.
        Dim dr As SqlDataReader = command.ExecuteReader()
        ' Retrives the schema of the table.
        Dim dtSchema As DataTable = dr.GetSchemaTable()
        ' Creates the CSV file as a stream, using the given encoding.
        Dim sw As New StreamWriter(fileOut, False, Me.encodingCSV)
        Dim dtTable As DataTable = dr.GetSchemaTable()
        '  string strRow; // represents a full row
        ' Writes the column headers if the user previously asked that.
        If Me.chkFirstRowColumnNames.Checked Then
            sw.WriteLine(columnNames(dtSchema, Me.separator))
        End If

        While dr.Read()
            RowsCount = RowsCount + 1
            Dim strRow As String = ""
            Dim sDummy As String = ""
            ' ProgressBar met pecentage aanwijzing
            ProgressBarX1.Value = (RowsCount / Counter) * (100)
            Dim percent As Integer = CInt(((CDbl(ProgressBarX1.Value) / CDbl(ProgressBarX1.Maximum)) * 100))
            ProgressBarX1.CreateGraphics().DrawString(percent.ToString() & "%", New System.Drawing.Font("Arial", CSng(8.25), FontStyle.Regular), Brushes.Black, New PointF(ProgressBarX1.Width / 2 - 10, ProgressBarX1.Height / 2 - 7))

            ' Reads the rows one by one from the SqlDataReader
            ' transfers them to a string with the given separator character and
            ' writes it to the file.
            For i As Integer = 0 To dr.FieldCount - 1
                Try
                    ' if Setting value to the sDummy variable blows up, we go catch...
                    sDummy = dr.GetValue(i)
                    ' In case the value on the field has a comma (,), put a "" around the value...
                    strRow += If(sDummy.IndexOf(","), """"c & dr.GetValue(i) & """"c, dr.GetValue(i))
                    If i >= dr.FieldCount - 1 Then
                        Continue For
                    End If
                    strRow += separator
                Catch
                    If i < dr.FieldCount - 1 Then
                        strRow += separator
                    End If
                End Try

            Next
            sw.WriteLine(strRow)
        End While
        ' Closes the text stream and the database connenction.
        sw.Close()
        conn.Close()
        ' Notifies the user.
        LabelX1.Text = Counter & " Rows copied ready!"
        ',MessageBox.Show("ready")
    End Sub

Hopen iemand hier een plezier mee te doen.


Sorry even counter vergeten

Dim Counter As Integer 'je dataset BindingSource
Counter = datasetBindingSource.Count
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan