Hi,
I hope someone can offer me some assistance in this query.
I am trying to export to excel from SQL Code.
I have got some code at the moment, but it takes about 50 minutes to export 24,000 rows. using this code
If i do a dts straight from SQL this takes about 7 minutes.
I think i need to use the Microsoft.Jet.OLEDB.4.0 drivers to allow me to do this.
Is this the best way or is there an easier way of doing this.
many thanks for any help in advance.
I hope someone can offer me some assistance in this query.
I am trying to export to excel from SQL Code.
I have got some code at the moment, but it takes about 50 minutes to export 24,000 rows. using this code
Code:
Public Overloads Shared Function fncExcelExport(ByVal datasource As String, ByVal database As String, ByVal userid As String, ByVal password As String, ByVal strSql As String, ByVal Filename As String)
Dim returnvalue As String = String.Empty
Dim returnex As Exception
Static Excel As New Excel.Application
Try
Dim dsExcelExport As New System.Data.DataSet
Dim daExcelExport As System.Data.SqlClient.SqlDataAdapter
Dim intColumn, intRow, intColumnValue As Integer
Dim strExcelFile As String
Dim conCurrent As New SqlClient.SqlConnection
conCurrent.ConnectionString = "data source=" & datasource & ";initial catalog=" & database & ";User ID=" & userid & ";Password=" & password
daExcelExport = New System.Data.SqlClient.SqlDataAdapter(strSql, conCurrent)
daExcelExport.Fill(dsExcelExport)
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
'For displaying the column name in the the excel file.
For intColumn = 0 To dsExcelExport.Tables(0).Columns.Count - 1
.Cells(1, intColumn + 1).Value = dsExcelExport.Tables(0).Columns(intColumn).ColumnName.ToString
Next
'For displaying the column value row-by-row in the the excel file.
For intRow = 1 To dsExcelExport.Tables(0).Rows.Count
For intColumnValue = 0 To dsExcelExport.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value = dsExcelExport.Tables(0).Rows(intRow - 1).ItemArray(intColumnValue).ToString
Next
Next
'strFileName = InputBox("Please enter the file name.", "Swapnil")
strExcelFile = Filename
.ActiveWorkbook().SaveAs(strExcelFile)
.ActiveWorkbook.Close()
End With
GC.Collect()
Excel.Quit()
returnvalue = "File exported sucessfully."
Catch ex As System.Runtime.InteropServices.COMException
returnex = ex
Excel.Quit()
Excel.Application.Quit()
Catch ex As SqlClient.SqlException
returnex = ex
Excel.Quit()
Excel.Application.Quit()
Catch ex As IndexOutOfRangeException
returnex = ex
Excel.Quit()
Excel.Application.Quit()
Catch ex As InvalidCastException
returnex = ex
Excel.Quit()
Excel.Application.Quit()
Catch ex As Exception
returnex = ex
Excel.Quit()
Excel.Application.Quit()
End Try
If returnex IsNot Nothing Then
Excel.Quit()
Return returnex
Else
Excel.Quit()
Return (returnvalue)
End If
End Function
If i do a dts straight from SQL this takes about 7 minutes.
I think i need to use the Microsoft.Jet.OLEDB.4.0 drivers to allow me to do this.
Is this the best way or is there an easier way of doing this.
many thanks for any help in advance.