Dim i, j As Integer
Dim xls As New Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
xls.Visible = True
Set book = xls.Workbooks.Add
Set sheet = book.Worksheets(1)
For i = 0 To AdodcData.Recordset.RecordCount - 1
For j = 0 To 7
DataGrid1.Row = i
DataGrid1.Col = j
sheet.Cells(2, j + 1) = DataGrid1.Columns(j).Caption
sheet.Cells(i + 3, j + 1) = DataGrid1.Text
Next j
Next i
执行过程中,输出一部分之后,会碰到执行到这一句报错:DataGrid1.Row = i,提示输出行错误,为什么阿,应该怎么解决?

解决方案 »

  1.   

    你可以将Datagrid的相关Rs中的数据导出到excel:
    http://www.microsoft.com/china/community/Column/32.mspx
      

  2.   

    cells中的下标是从1开始的,不是0.修改一下试试:For i = 1 To AdodcData.Recordset.RecordCount
    For 1 = 1 To 8
    DataGrid1.Row = i
    DataGrid1.Col = j
    sheet.Cells(2, j + 1) = DataGrid1.Columns(j).Caption
    sheet.Cells(i + 3, j + 1) = DataGrid1.Text
    Next j
    Next i
      

  3.   

    我注意到了cells的下标,从1开始,输出到大约1/3的时候出错了