Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Export to Excel (slow)
Message
From
24/04/2007 11:29:31
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
24/04/2007 10:44:25
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01219337
Message ID:
01219369
Views:
7
>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()
Either copy the data into clipboard and paste it or use a recordset to transfer data (CopyFromRecordset - or alternatively QueryTables.Add). It takes 1 or 2 seconds.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform