15-10-18, 03:22 AM
(15-10-18, 12:35 AM)asemshahen5 كتب :كود :
Public Sub ExportToExcel_DT(ByVal dtTemp As System.Data.DataTable, ByVal FilePath As String)
If IO.File.Exists(FilePath) Then
If MsgBox("الملف موجود مسبقا هل تريد حذفه", MsgBoxStyle.YesNo, "ملف موجود") = MsgBoxResult.Yes Then
IO.File.Delete(FilePath)
Else
Return
End If
End If
Dim _Excel As New Excel.Application
Dim WBook As Excel.Workbook
Dim WSheet As Excel.Worksheet
WBook = _Excel.Workbooks.Add
WSheet = WBook.ActiveSheet
WSheet.DisplayRightToLeft = True
Dim DT As System.Data.DataTable = dtTemp
Dim DC As DataColumn
Dim DR As DataRow
Dim ColIndex As Integer = 0
Dim RowIndex As Integer = 0
For Each DC In DT.Columns
ColIndex = ColIndex + 1
WSheet.Cells(1, ColIndex) = DC.ColumnName
Next
For Each DR In DT.Rows
RowIndex = RowIndex + 1
ColIndex = 0
For Each DC In DT.Columns
ColIndex = ColIndex + 1
WSheet.Cells(RowIndex + 1, ColIndex) = DR(DC.ColumnName)
'-------------------------------------------------------
'-------------------------------------------------------
Next
Next
WSheet.Columns.AutoFit()
WBook.SaveAs(FilePath)
WSheet = Nothing
WBook.Close(False)
WBook = Nothing
_Excel.Quit()
_Excel = Nothing
GC.Collect()
MsgBox("تمت عملية تصدير البيانات الى اكسل بنجاح", MsgBoxStyle.Information, "نجاح التصدير")
End Sub
كود :
Dim StrPath As String = "D:\Excel.xlsx"
If dt.Rows.Count > 0 Then
ExportToExcel_DT(dt, StrPath)
End If
اعجز عن الشكر اخي asemshahen5
يسرا اللَّه لك أمرك في الدنيا والأخرة

