19-01-17, 09:34 PM
Dim exlapp As New Application
Dim exlworkbook As Workbook
Dim exlworksheet As Worksheet
Dim misvalue As Object = System.Reflection.Missing.Value
exlworkbook = exlapp.Workbooks.Add(misvalue)
exlworksheet = exlworkbook.Sheets("Sheet1")
For colhead As Integer = 1 To DataGridView1.ColumnCount - 1
exlworksheet.Cells(1, colhead + 1) = DataGridView1.Columns(colhead).HeaderText
Next
For i As Integer = 0 To DataGridView1.RowCount - 1
For j As Integer = 1 To DataGridView1.ColumnCount - 1
exlworksheet.Cells(i + 2, j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString
Next
Next
sfd1.FileName = ""
sfd1.Filter = "Excel 2003| *.xls | Excel 2007| *.xlsx "
If sfd1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
exlworksheet.SaveAs(sfd1.FileName)
Else
Exit Sub
End If
exlworkbook.Close()
exlapp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlapp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlworkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlworksheet)
exlapp = Nothing
exlworkbook = Nothing
exlworksheet = Nothing
If MsgBox("Do You Want To Open File ", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Process.Start(sfd1.FileName)
End If
ده كود شغال معايا تمام وبيخرج الاكسل ومفيش اي مشكلة جربو
Dim exlworkbook As Workbook
Dim exlworksheet As Worksheet
Dim misvalue As Object = System.Reflection.Missing.Value
exlworkbook = exlapp.Workbooks.Add(misvalue)
exlworksheet = exlworkbook.Sheets("Sheet1")
For colhead As Integer = 1 To DataGridView1.ColumnCount - 1
exlworksheet.Cells(1, colhead + 1) = DataGridView1.Columns(colhead).HeaderText
Next
For i As Integer = 0 To DataGridView1.RowCount - 1
For j As Integer = 1 To DataGridView1.ColumnCount - 1
exlworksheet.Cells(i + 2, j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString
Next
Next
sfd1.FileName = ""
sfd1.Filter = "Excel 2003| *.xls | Excel 2007| *.xlsx "
If sfd1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
exlworksheet.SaveAs(sfd1.FileName)
Else
Exit Sub
End If
exlworkbook.Close()
exlapp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlapp)
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlworkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(exlworksheet)
exlapp = Nothing
exlworkbook = Nothing
exlworksheet = Nothing
If MsgBox("Do You Want To Open File ", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Process.Start(sfd1.FileName)
End If
ده كود شغال معايا تمام وبيخرج الاكسل ومفيش اي مشكلة جربو
