VB6.0 连接Access数据库后 如何编写代码使数据库导出到excel、word或html上?
展开全部
Private Sub Command1_Click()
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlSheet As Variant
Dim sum As Long
Set xlapp = CreateObject("excel.application")
Set xlBook = xlapp.Workbooks.Open(App.Path & "\data\报表.xlt") '打开EXCEL模板
'Set xlBook = xlapp.Workbooks.Add
Set xlSheet = xlBook.worksheets(1)
xlapp.Visible = True
Adodc1.ConnectionString = cn.ConnectionString
Adodc1.RecordSource = "select * from [Sheet1] where 试验号='" & shiyanH & "'"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
Adodc1.Recordset.MoveFirst
xlSheet.Cells(sum + 1, 2) = shiyanH
For sum = 0 To Adodc1.Recordset.RecordCount - 1
xlSheet.Cells(sum + 3, 1) = Adodc1.Recordset(1)
For j = 2 To 21
If Adodc1.Recordset(j) <> "" Then
If Adodc1.Recordset(j) = "********" Then
xlSheet.Cells(sum + 3, j) = (Adodc1.Recordset(j))
Else
xlSheet.Cells(sum + 3, j) = Val(Adodc1.Recordset(j))
End If
End If
Next
Adodc1.Recordset.MoveNext
Next sum
End If
End Sub
Sub Main()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\tsdb.mdb;Persist Security Info=False"
frmStart.Show
End Sub
以上是读取Access数据表,导入到Excel的代码。
更多追问追答
追问
您好!请问这个代码是不是需要先引用Microsoft Excel部件?
还有就是第12行的“试验号”是不是要改成我自己数据库的名称,还有第12行和第16行的shiyanH是Excel里sheet1的名字吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询