写了个小程序,VB6+2000环境,数据库用到了ADO 和DATAGRID,datagrid查询结果配合一个command按钮加了段输出到EXCEL的代码,可是只能输出当前datagrid显示的一页,超出页部份不输出,手动翻到datagrid下一页再次运行才行,而且是又输出到一个新文件,看看怎么样把datagrid里所有的查询结果全部一次输出到excel,帮帮忙啦,调试可行的话另外加分~~
代码如下:
Private Sub Command1_Click()
Dim i, j As Integer
Dim xlApplication As Excel.Application, xlWorkbook As Excel.Workbook, xlSheet
On Error Resume Next
Set xlApplication = GetObject(, "Excel.Application")
If Err.Number <> 0 Then Set xlApplication = CreateObject("Excel.Application")
Set xlWorkbook = xlApplication.Workbooks.Add
Set xlSheet = xlWorkbook.ActiveSheet
For i = 1 To DataGrid1.Columns.Count
xlSheet.Cells(1, i) = DataGrid1.Columns(i - 1).Caption
For j = 0 To DataGrid1.VisibleRows - 1
xlSheet.Cells(j + 2, i) = DataGrid1.Columns(i - 1).CellText(DataGrid1.RowBookmark(j))
Next j
Next i
xlApplication.Visible = True
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApplication = Nothing
End Sub