vb6的導出到xecel的函數
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Columns.AutoFit
Me.MousePointer = 11
For k = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列數
xlSheet.Cells(1, k + 1) = DataGrid1.Columns(k).Caption '第一行為DataGrid的列標題
Next
DataGrid1.Scroll 0, -DataGrid1.FirstRow '導出前拉動過垂直滾動條,這個非常重要
DataGrid1.Row = 0
For i = 0 To DataGrid1.ApproxCount - 1 'DataGrid的所有行數
For j = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列數,若將此數改小到不拉DataGrid的垂直滾動條的時候能看見的行數的時候正常
DataGrid1.Col = j
xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j) 'DataGrid1.Text '從第二行顯示'DataGrid的內容
Next
If i < DataGrid1.ApproxCount - 1 Then
DataGrid1.Row = DataGrid1.Row + 1
End If
Next
Me.MousePointer = 0
MsgBox "導出成功!"
xlApp.Visible = True
Set xlApp = Nothing 'Excel 處於當前窗體
Set xlBook = Nothing
Set xlSheet = Nothing
End Sub