What is the fastest way to export DataGridView rows in the range of 460328 - 800328 to Excel or into an SQL Server database table with out using Microsoft office interop as
For transferring to Excel, this is the fastest method that I've found (although it does use Office InterOp). Loop through each cell in the DataGridView and assign it to an array of objects. Then assign the entire array to an Excel Range. This is much faster than assigning a value to each Excel cell individually because it only invokes InterOp once. Pardon the VB:
Sub Export()
Dim xlApp As New Excel.Application
Dim wb As Excel.Workbook = xlApp.Workbooks.Add
Dim ws As Excel.Worksheet = wb.Worksheets(1)
Dim dgv as DataGridView = MyDataGridView
Dim ExportArray(dgv.Rows.Count, dgv.Columns.Count - 1) As Object
Dim j, i As Integer
For j = 0 To dgv.Columns.Count - 1
ExportArray(0, j) = dgv.Columns(j).Name
For i = 1 To dgv.Rows.Count
ExportArray(i, j) = dgv(j, i - 1).Value
Next
Next
Dim col As String = ColNumtoLetter(j)
ws.Range("A1:" & col & i).Value = ExportArray
End Sub
Private Function ColNumtoLetter(ByVal iCol As Integer) As String
Dim Result As String = ""
Dim iAlpha As Integer = Int(iCol / 26.001)
Dim iRemainder As Integer = iCol - (iAlpha * 26)
If iAlpha > 0 Then
Result = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
Result = Result & Chr(iRemainder + 64)
End If
Return Result
End Function
The second method just translates the final column number to the corresponding Excel column name.
See "Fast Exporting from DataSet to Excel" and "Export Data to Excel Much Faster" for more info.