我用查询的记录集导出生成了一个Excel表格,列名有,可是记录集也就是内容却是空的
但是记录集肯定查询出来了.代码如下,望高人指点 On Error GoTo gherr
Dim icol As Integer '列数,用于保存字段个数
Dim ijlts As Long '记录条数
Dim yesorno As Long '确认或是取消的标志 Dim AppExcel As Excel.Application '定义
Dim BookExcel As Excel.Workbook '工作簿对象
Dim sheetexcel As Excel.Worksheet '工作表
''---------取出记录集的行和列数----------
With rs
If .RecordCount = 0 Then
MsgBox ("没有记录可供导出,该操作已经取消!")
Exit Sub
Else
icol = .Fields.Count '求字段数
ijlts = .RecordCount '求记录数
Debug.Print "----"
Debug.Print icol
Debug.Print ijlts
End If
End With
Set AppExcel = New Excel.Application '创建excel对象
Set BookExcel = AppExcel.Workbooks.Add '添加工作簿
Set sheetexcel = BookExcel.Worksheets("sheet1") '添加工作表
For icol = 0 To rs.Fields.Count - 1
AppExcel.Worksheets(1).Cells(1, icol + 1).Value = rs.Fields(icol).Name
Next
AppExcel.Worksheets(1).Range("A2").CopyFromRecordset rs BookExcel.SaveAs (App.Path + "..\Excel\Details.xls") AppExcel.Quit
Set sheetexcel = Nothing
Set BookExcel = Nothing
Set AppExcel = Nothing
Exit Sub
gherr:
MsgBox "由于未知原因,导出失败!", vbQuestion
但是记录集肯定查询出来了.代码如下,望高人指点 On Error GoTo gherr
Dim icol As Integer '列数,用于保存字段个数
Dim ijlts As Long '记录条数
Dim yesorno As Long '确认或是取消的标志 Dim AppExcel As Excel.Application '定义
Dim BookExcel As Excel.Workbook '工作簿对象
Dim sheetexcel As Excel.Worksheet '工作表
''---------取出记录集的行和列数----------
With rs
If .RecordCount = 0 Then
MsgBox ("没有记录可供导出,该操作已经取消!")
Exit Sub
Else
icol = .Fields.Count '求字段数
ijlts = .RecordCount '求记录数
Debug.Print "----"
Debug.Print icol
Debug.Print ijlts
End If
End With
Set AppExcel = New Excel.Application '创建excel对象
Set BookExcel = AppExcel.Workbooks.Add '添加工作簿
Set sheetexcel = BookExcel.Worksheets("sheet1") '添加工作表
For icol = 0 To rs.Fields.Count - 1
AppExcel.Worksheets(1).Cells(1, icol + 1).Value = rs.Fields(icol).Name
Next
AppExcel.Worksheets(1).Range("A2").CopyFromRecordset rs BookExcel.SaveAs (App.Path + "..\Excel\Details.xls") AppExcel.Quit
Set sheetexcel = Nothing
Set BookExcel = Nothing
Set AppExcel = Nothing
Exit Sub
gherr:
MsgBox "由于未知原因,导出失败!", vbQuestion
AppExcel.Worksheets(1).Cells(1, icol + 1).Value = rs.Fields(icol).Name
Next
改为:
For icol = 0 To rs.Fields.Count - 1
sheetexcel.Cells(1, icol + 1).Value = rs.Fields(icol).Name
Next
试一下
按你说的改过了,还是不行啊!!!Excel表仍然是空的
那位高人指点指点