Dim i As Integer
Dim j As Integer
Dim ex As Object
Dim exwbook As Object
Dim exsheet As Object
Set ex = CreateObject("Excel.Application")
Set exwbook = Nothing
Set exsheet = Nothing
Set exwbook = ex.Workbooks().Add
Set exsheet = exwbook.Worksheets("sheet1")
'在excel里表格的表头
ex.Range("c3").Value = "序号1"
ex.Range("d3").Value = "时间"
ex.Range("e3").Value = "数据1"
ex.Range("f3").Value = "数据2"
ex.Range("g3").Value = "数据3"
ex.Range("h3").Value = "累计数据"
'i为记录个数使用循环将数据全部添加
For i = 1 To Adodc1.Recordset.RecordCount
j = 3 + i
'k为数据列数
For k = 0 To 5
'通过使用变量k和j变换单元格位置
q = Chr(99 + k) & j
'将datagrid1的数据放到单元格内
ex.Range(q).Value = DataGrid1.Columns(k)
Next k
'指针下移
If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
End If
Next i
'保存输入到信息查询结果.xls
exwbook.SaveAs "D:\新建文件夹\记录.xls"
'退出excel
ex.Quit
End Sub
使用ADO对象。