public Sub makeexcel(ByVal res As ADODB.Recordset, ByVal savepath As String)On Error GoTo errhandlerDim xlapp As New Excel.Application
Dim xlwbook As New Excel.Workbook
Dim xlwsheet As New Excel.Worksheet
Dim icol As Integer
Dim irow As IntegerIf res.BOF And res.EOF Then
Exit Sub
End IfSet xlwbook = xlapp.Workbooks.Add
Set xlsheet = xlapp.Worksheets.Add
irow = 1
icol = 0For icol = 0 To res.Fields.Count - 1
xlwsheet.Cells(1, icol + 1) = res.Fields(icol).Name
Next
res.MoveFirst
While Not res.EOF
irow = irow + 1
For icol = 0 To res.Fields.Count - 1
xlwsheet.Cells(irow, irow + 1) = res.Fields(icol).Value
Next
res.MoveNext
Wendxlwsheet.SaveAs savepathxlwbook.Close
xlapp.Quit
Set res = Nothing
Set xlwbook = Nothing
Set xlwsheet = Nothing
Set xlwapp = NothingExit Sub
errhandler:
Err.Raise Err.Number, Err.Source, Err.Description
xlwbook.Close
xlapp.Quit
Set res = Nothing
Set xlwsheet = Nothing
Set xlwbook = Nothing
Set xlapp = NothingEnd Sub
Private Sub Command6_Click()
Dim conn As New ADODB.Connection
Dim res As ADODB.Recordset
conn.Open "pb"
Set res = conn.Execute("select * from box")
makeexcel res, App.Path & "\2.xls"
Set res = Nothing
Set conn = Nothing
MsgBox "报表已生成"
End Sub
这是我将查询结果导入excel里的一个程序,运行结果老是显示"类不支持自动化或不支持期望的接口"怎么回事?请指教!
Dim xlwbook As New Excel.Workbook
Dim xlwsheet As New Excel.Worksheet
Dim icol As Integer
Dim irow As IntegerIf res.BOF And res.EOF Then
Exit Sub
End IfSet xlwbook = xlapp.Workbooks.Add
Set xlsheet = xlapp.Worksheets.Add
irow = 1
icol = 0For icol = 0 To res.Fields.Count - 1
xlwsheet.Cells(1, icol + 1) = res.Fields(icol).Name
Next
res.MoveFirst
While Not res.EOF
irow = irow + 1
For icol = 0 To res.Fields.Count - 1
xlwsheet.Cells(irow, irow + 1) = res.Fields(icol).Value
Next
res.MoveNext
Wendxlwsheet.SaveAs savepathxlwbook.Close
xlapp.Quit
Set res = Nothing
Set xlwbook = Nothing
Set xlwsheet = Nothing
Set xlwapp = NothingExit Sub
errhandler:
Err.Raise Err.Number, Err.Source, Err.Description
xlwbook.Close
xlapp.Quit
Set res = Nothing
Set xlwsheet = Nothing
Set xlwbook = Nothing
Set xlapp = NothingEnd Sub
Private Sub Command6_Click()
Dim conn As New ADODB.Connection
Dim res As ADODB.Recordset
conn.Open "pb"
Set res = conn.Execute("select * from box")
makeexcel res, App.Path & "\2.xls"
Set res = Nothing
Set conn = Nothing
MsgBox "报表已生成"
End Sub
这是我将查询结果导入excel里的一个程序,运行结果老是显示"类不支持自动化或不支持期望的接口"怎么回事?请指教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货