Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Export to Excel (slow)
Message
De
24/04/2007 10:44:25
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Export to Excel (slow)
Divers
Thread ID:
01219337
Message ID:
01219337
Vues:
69
I am using vb.net to export the data to excel...
By using Do While Looping .insert each row one by one.
It seems very very slow to insert 5000 records (over 20 mins)
Can simple method that I can export to excel in SQL server ...???
 Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
     & "Data Source=" & workbookFileName & ";Extended Properties=Excel 8.0"

    'Create an OleDb connection object
    Dim con As New System.Data.OleDb.OleDbConnection(connectionString)

    'enumerate through the rows of the data table and insert into the appropriate
    'Excel worksheet based on the breakCount value.
    Dim breakCount As Int32 = 65535
    Dim sheetIndex As Int32 = 1
    Dim recordIndex As Int32 = 0
    Dim sheetName As String = "Sheet" & sheetIndex
    Dim insertStatement As System.Text.StringBuilder
    Dim command As System.Data.OleDb.OleDbCommand

    con.Open()

    For Each row As DataRow In data.Rows

        recordIndex += 1
    If recordIndex = breakCount Then
     sheetIndex += 1
     sheetName = "Sheet" & sheetIndex.ToString
    End If

        'Write the Insert Statement for the current record
    insertStatement = New System.Text.StringBuilder
    insertStatement.Append("INSERT INTO [" & sheetName & "$] VALUES (")

    For Each col As DataColumn In data.Columns
            insertStatement.Append("'" & row(col.ColumnName).ToString.Replace("'", "''") & "', ")
    Next

    command = New System.Data.OleDb.OleDbCommand

    Try
            With command
     .Connection = con
        .CommandText = insertStatement.ToString.Substring(0, insertStatement.ToString.LastIndexOf(Char.Parse(","))) & ")"
        .CommandType = CommandType.Text
        .ExecuteNonQuery()
     End With
    Catch ex As System.Data.OleDb.OleDbException
     MessageBox.Show(ex.Message)
    End Try

    Next

    con.Dispose()
    command.Dispose()
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform