Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim MsExcel = CreateObject("Excel.Application")
MsExcel.Workbooks.Add()
MsExcel.Visible = False
Dim k As New ArrayList
If CheckBox1.Checked Then k.Add(0)
If CheckBox2.Checked Then k.Add(1)
If CheckBox3.Checked Then k.Add(2)
Dim g As Integer
g = 0
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If Not k.Contains(i) Then Continue For
g = g + 1
'MsExcel.Cells(i + 1).Value = DataGridView1.Columns(i).HeaderText
MsExcel.Cells(g).Value = DataGridView1.Columns(i).HeaderText
Next
g = 0
For i As Integer = 0 To DataGridView1.Columns.Count - 1
If Not k.Contains(i) Then Continue For
g = g + 1
For j As Integer = 0 To DataGridView1.Rows.Count - 1
MsExcel.Columns.HorizontalAlignment = 3
MsExcel.Columns.Font.Name = "Times New Roman"
'MsExcel.Rows.Item(j + 1).Font.Bold = 1
'MsExcel.Rows.Item(j + 1).Font.size = 14
MsExcel.Cells(j + 1).ColumnWidth = 14
'MsExcel.Cells(j + 2, i + 1).Value = DataGridView1.Rows(j).Cells(i).Value
MsExcel.Cells(j + 2, g).Value = DataGridView1.Rows(j).Cells(i).Value
Next
Next
If MsgBox("هل تريد فتح الملف", MsgBoxStyle.YesNo, "تصدير البيانات") = MsgBoxResult.Yes Then
'frm_excel.TopMost = False
MsExcel.Visible = True
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub