Dim i, j, o As Integer
o = DataGridView1.Columns.Count - 1
If o < 0 Then
MessageBox.Show("没数据可导到Excel哦!", Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End If
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Add
xlSheet = xlBook.Worksheets(1)
xlApp.Visible = True
xlApp.Columns("c:c").NumberFormatLocal = "@"
xlApp.Columns("e:e").NumberFormatLocal = "@"
xlApp.Columns("j:j").NumberFormatLocal = "@"
For i = 0 To o
xlSheet.Cells(1, i + 1).Value = DataGridView1.Columns(i).HeaderText
xlApp.Cells(1, i + 1).HorizontalAlignment = 3
Next
With DataGridView1
For i = 0 To .Rows.Count - 1
For j = 0 To .ColumnCount - 1
xlSheet.Cells(i + 2, j + 1).Value = .Rows(i).Cells(j).Value
Next j
xlSheet.Range("a" & i + 2).Select()
Next i
End With
xlApp.Columns("a:at").EntireColumn.AutoFit()
xlSheet.Range("a1").Select()
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing