发表于:2004-02-16 13:55:00
楼主
我Adodc1和DataGrid1用2个控件完成了对SQL数据库的查询功能,现在想用一个命令按钮Command1实现将查询结果发送到Excel2000中的功能,程序代码已经写好,也将Microsoft Excel 9.0 Object Library导入了工程1中,但运行时,如果查询结果的记录总数比较少,可以正常发送到Excel表中,当查询结果的记录数比较多时,发送10来个数据后,程序就出现错误了,这是怎么回事?请高手指点指点我,谢了!!!
还有,存入Excel表后,各列的名字能自动改吗?现在显示的是A1,B1,C1,,,,,,而不是日期,流量,液位,压力,,,,,我改怎么做???
我对按钮Command1编程如下:
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.Worksheets(1)
For i = 0 To Adodc1.Recordset.RecordCount - 1
For j = 0 To Adodc1.Recordset.Fields.Count - 1
DataGrid1.Row = i (编译后,运行一会就提示出错的语句)
DataGrid1.Col = j
xlsheet.Cells(i + 1, j + 1) = DataGrid1.Text
Next j
Next i
Set xlapp = Nothing
Set xlbook = Nothing
End Sub