vb新建excel小弟我会了,在form1的text1里显示selet * from table1小弟我也会了,现在要用vb来生成excel,excel里的数据就是selet * from table1

vb新建excel我会了,在form1的text1里显示selet * from table1我也会了,现在要用vb来生成excel,excel里的数据就是selet * from table1,
vb新建excel我会了,在form1的text1里显示selet   *   from   table1我也会了,现在要用vb来生成excel,excel里的数据就是selet   *   from   table1,用ado来做,这两个并起来怎么做,希望写点属性啊,例子之类的



------解决方案--------------------
'**************************************
' 过程名: Recordset2Excel
' 描 述:使用ADO Recordset对象把记录导入到Excel文件中
'
' 输 入:ADO Recordset
'
'****************************************


Public Sub Recordset2Excel(rstSource As ADODB.Recordset)
Dim xlsApp As Excel.Application
Dim xlsWBook As Excel.Workbook
Dim xlsWSheet As Excel.Worksheet
Dim i, j As Integer

' 获取或者建立 Excel 对象
On Error Resume Next
Set xlsApp = GetObject(, "Excel.Application ")


If Err.Number <> 0 Then
Set xlsApp = New Excel.Application
Err.Clear
End If

' 建立 WorkSheet
Set xlsWBook = xlsApp.Workbooks.Add
Set xlsWSheet = xlsWBook.ActiveSheet

' 导出 ColumnHeaders


For j = 0 To rstSource.Fields.Count
xlsWSheet.Cells(2, j + 1) = rstSource.Fields(j).Name
Next j

' 导出 Data
rstSource.MoveFirst


For i = 1 To rstSource.RecordCount


For j = 0 To rstSource.Fields.Count
xlsWSheet.Cells(i + 2, j + 1) = rstSource.Fields(j).Value
Next j
rstSource.MoveNext
Next i
rstSource.MoveFirst
' 自适应行标题


For i = 1 To rstSource.Fields.Count
xlsWSheet.Columns(i).AutoFit
Next i

xlsWSheet.Range( "A1 ").Select


' 显示 Excel
xlsApp.Visible = True

Set xlsApp = Nothing
Set xlsWBook = Nothing
Set xlsWSheet = Nothing
End Sub

*****************************************************************************
欢迎使用****论坛阅读器 : **** Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
rstSource.RecordCount =?



*****************************************************************************
欢迎使用****论坛阅读器 : **** Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
-1是因为打开recordset的方式问题
你可以改成
while not rstSource.eof

For j = 0 To rstSource.Fields.Count
xlsWSheet.Cells(i + 1, j + 1) = rstSource.Fields(j).Value
Next j
rstSource.MoveNext
wend

CopyFromRecordset 好像对recordset内容有一些限制

*****************************************************************************
欢迎使用****论坛阅读器 : **** Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
xlSheet.cells(3,1).copyfromrecordset rstSource
------解决方案--------------------

Dim xlsApp As Excel.Application
Dim xlsBook As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
Set xlsApp = New Excel.Application
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets(1)
查詢數據
While Not AdoRst.EOF