返回VB界面,点击“工程”→“引用”→选择[Microsoft Excel9.0 object Library]
复制下列代码,绝对正确
Dim i As Integer, j%
Dim Newxls As Excel.Application
Dim Newbook As Excel.Workbook
Dim Newsheet As Excel.Worksheet
Set Newxls = CreateObject("excel.application") ''创建Excel应用程序并打开之
Newxls.Visible = True
Set Newbook = Newxls.Workbooks.Add ''创建工作簿
Set Newsheet = Newbook.Worksheets(1) ''创建工作表
If Not Adodc4.Recordset.EOF Then Adodc4.Recordset.MoveFirst
For i = 0 To Adodc4.Recordset.RecordCount - 1
For j = 0 To Adodc4.Recordset.Fields.Count - 1
DataGrid1.Row = i
DataGrid1.Col = j
Newsheet.Cells(1, j + 1) = DataGrid1.Columns(j).Caption ''显示DataGrid1标题
Newsheet.Cells(i + 2, j + 1) = DataGrid1.Text
Next j
Next i