The code below doing the job but it takes almost 60 seconds to complete execution. there are about 2000 rows and 55 columns. I guess a nested loop is not the most efficient and professional way, I searched in Google but haven't found a better way...(I thought maybe there is a way to use SQL?). I will be thankful for suggestions of making this task more efficient. (its written in vb.net but c# is fine also for examples and suggestions)
Public Sub LoadDataTableToExcell(ByVal d As DataTable, ByVal path As String, ByVal fileName As String, ByVal newFile As Boolean, ByVal sheetName As String)
If CheckIfDirExist(path) = False Then
MsgBox("the Path" & " " & path & " " & "Does not exist")
Exit Sub
End If
CreateAnExcelFile(path, sheetName)
Dim xlapp As New Excel.Application
Dim ws As Excel.Worksheet
Dim xlworkbook As Excel.Workbook
xlworkbook = xlapp.Workbooks.Open(path)
ws = xlworkbook.Sheets(sheetName)
Dim Erows As Integer = Nothing
Dim Ecolumns As Integer = Nothing
For Ecolumns = 0 To d.Columns.Count - 1
ws.Cells(1, Ecolumns + 1) = d.Columns(Ecolumns).ColumnName
For Erows = 0 To d.Rows.Count - 1
ws.Cells(Erows + 3, Ecolumns + 1) = d.Rows(Erows).Item(Ecolumns)
Next
Next
Dim CarbonNewRng As Excel.Range
CarbonNewRng = ws.Range(ws.Cells(1, 1), ws.Cells(d.Rows.Count + 2, d.Columns.Count))
CarbonNewRng.Borders.Weight = 4
CarbonNewRng.Borders.Color = Color.FromArgb(0, 64, 64)
ws.Columns("A:AZ").autofit()
xlworkbook.Save()
xlworkbook.Close()
xlapp.Quit()
releaseObject(xlapp)
releaseObject(ws)
releaseObject(xlworkbook)
xlworkbook = Nothing
xlapp = Nothing
ws = Nothing
Else
End If
end sub