vb datagrid 导出到excel 行号无效

来源:百度知道 编辑:UC知道 时间:2024/06/08 20:03:56
Dim i As Integer
Dim j As Integer
Dim z 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
'On Error Resume Next
Set xlBook = xlApp.Workbooks.Add(App.Path + "\ruhui.xls")
Set xlSheet = xlBook.Worksheets(1)
For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(1, j + 1) = DataGrid1.Columns.Item(j).Caption
Next j
xlSheet.Cells(6, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
MsgBox i
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text

If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text
End If
Next j

干吗非得从datagrid的表格里面一个个读取数据再放到Excel中呢?从adodc1中直接读取不是更方便快捷吗?

......
Adodc1.Recordset.MoveFirst
For i = 0 To Adodc1.Recordset.RecordCount - 1
For j = 0 To Adodc1.Recordset.Fields.Count - 1
xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j) & ""
Next j
Adodc1.Recordset.MoveNext
Next i
......